Skip to content

Commit 5d11a18

Browse files
committed
Add compile-time config
1 parent 07be6a5 commit 5d11a18

9 files changed

+28
-21
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ CMakeCache.txt
99
CMakeFiles
1010
cmake_install.cmake
1111
install_manifest.txt
12+
Makefile
13+
config.h

CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
cmake_minimum_required (VERSION 2.6)
2+
23
project (Intern)
4+
5+
set (PAGE_SIZE 4096 CACHE STRING "Page size for allocations")
6+
option (MMAP_PAGES "Allocate pages with mmap(2)" OFF)
7+
option (INLINE_UNSIGNED "Inline unsigned integers into the ID" OFF)
8+
9+
configure_file (config.h.in config.h)
10+
311
add_library (intern strings.c block.c optimize.c)
12+
413
install (TARGETS intern DESTINATION lib)
514
install (FILES strings.h block.h optimize.h DESTINATION include/intern)

block.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdlib.h>
22
#include <string.h>
33

4+
#include "config.h"
45
#include "block.h"
56
#include "branch.h"
67
#include "page.h"

config.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#define PAGE_SIZE @PAGE_SIZE@
2+
#cmakedefine MMAP_PAGES
3+
#cmakedefine INLINE_UNSIGNED

optimize.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdlib.h>
22
#include <string.h>
33

4+
#include "config.h"
45
#include "optimize.h"
56
#include "branch.h"
67

@@ -125,7 +126,7 @@ bool strings_frequency_add_all(struct strings_frequency *frequency,
125126
return true;
126127
}
127128

128-
struct strings *strings_optimize(const struct strings *strings,
129+
struct strings *strings_optimize(struct strings *strings,
129130
struct strings_frequency *frequency) {
130131
struct strings *optimized = strings_new();
131132
if (!optimized) {

optimize.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ struct strings_frequency;
88
// Create a new, optimized string repository which stores the most frequently
99
// seen strings together. The string with the lowest ID (1) is the most
1010
// frequently seen string
11-
struct strings *strings_optimize(const struct strings*,
12-
struct strings_frequency*);
11+
struct strings *strings_optimize(struct strings*, struct strings_frequency*);
1312

1413
// Create a new string frequency tracker
1514
struct strings_frequency *strings_frequency_new(void);

page.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#ifdef PAGE_MMAP
1+
#ifdef MMAP_PAGES
22
#include <sys/mman.h>
33

44
static inline void *page_alloc(size_t page_size) {

strings.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string.h>
33
#include <limits.h>
44

5+
#include "config.h"
56
#include "strings.h"
67
#include "tree.h"
78
#include "branch.h"
@@ -228,14 +229,12 @@ uint32_t strings_lookup(const struct strings *strings, const char *string) {
228229
return 0;
229230
}
230231

231-
#ifdef INLINE_UNSIGNED
232232
const char *strings_lookup_id(struct strings *strings, uint32_t id) {
233+
#ifdef INLINE_UNSIGNED
233234
if (id & unsigned_tag) {
234235
unsigned_string(strings->buffer, id & ~unsigned_tag);
235236
return strings->buffer;
236237
}
237-
#else
238-
const char *strings_lookup_id(const struct strings *strings, uint32_t id) {
239238
#endif
240239

241240
if (UNLIKELY(id > strings->total)) {

strings.h

+7-14
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55

66
#include "block.h"
77

8-
#ifndef PAGE_SIZE
9-
# define PAGE_SIZE 4096
10-
#endif
11-
128
struct strings;
139

1410
// Create a new repository of strings
@@ -22,7 +18,8 @@ uint32_t strings_count(const struct strings*);
2218

2319
// Intern a string and get back a unique ID. Strings must be NULL-terminated.
2420
// Note that IDs start at 1 and increment to either UINT_MAX or INT_MAX if
25-
// INLINE_UNSIGNED is defined. This function returns 0 if an error occurred
21+
// INLINE_UNSIGNED was defined at compile time. This function returns 0 if an
22+
// error occurred
2623
uint32_t strings_intern(struct strings*, const char *string);
2724

2825
// Lookup the ID for a string. This function returns zero if the string
@@ -31,15 +28,11 @@ uint32_t strings_lookup(const struct strings*, const char *string);
3128

3229
// Lookup the string associated with an ID. This function returns NULL
3330
// if there is no string with that ID in the repository. If INLINE_UNSIGNED
34-
// is defined then the repository will inline unsigned integers into the ID
35-
// itself. When looking up the string associated with such an ID, the integer
36-
// is written into an internal buffer which persists until the function is
37-
// called again
38-
#ifdef INLINE_UNSIGNED
39-
const char *strings_lookup_id(struct strings*, uint32_t id);
40-
#else
41-
const char *strings_lookup_id(const struct strings*, uint32_t id);
42-
#endif
31+
// was defined at compile time then the repository will inline unsigned
32+
// integers into the ID itself. When looking up the string associated with such
33+
// an ID, the integer is written into an internal buffer which persists until
34+
// the function is called again
35+
const char *strings_lookup_id(struct strings*, uint32_t id);
4336

4437
struct strings_cursor {
4538
const struct block *strings;

0 commit comments

Comments
 (0)