Browse Source

[core] move data_{array,integer,string} to array.c

move native data_* types into array.c
(the types are already declared in array.h)

The array data structure remains extendable, as is done with data_config
(configfile) and data_auth (mod_auth), though array data structure
primary uses are at startup (config time) and header parsing.  The
insertion logic into sorted list can be expensive for large lists,
so header parsing might choose a different data structure in the future.
master
Glenn Strauss 5 months ago
parent
commit
4f8f83ea1d
  1. 19
      src/CMakeLists.txt
  2. 18
      src/Makefile.am
  3. 2
      src/SConscript
  4. 123
      src/array.c
  5. 8
      src/array.h
  6. 14
      src/configparser.y
  7. 51
      src/data_array.c
  8. 5
      src/data_config.c
  9. 52
      src/data_integer.c
  10. 58
      src/data_string.c
  11. 19
      src/meson.build

19
src/CMakeLists.txt

@ -752,8 +752,6 @@ set(COMMON_SRC
http_header.c http_kv.c keyvalue.c chunk.c
http_chunk.c fdevent.c gw_backend.c
stat_cache.c plugin.c http_etag.c array.c
data_string.c data_array.c
data_integer.c
algo_hmac.c algo_md5.c algo_sha1.c algo_splaytree.c
fdevent_select.c fdevent_libev.c
fdevent_poll.c fdevent_linux_sysepoll.c
@ -849,9 +847,6 @@ add_and_install_library(mod_wstunnel mod_wstunnel.c)
add_executable(test_array
t/test_array.c
array.c
data_array.c
data_integer.c
data_string.c
buffer.c
)
add_test(NAME test_array COMMAND test_array)
@ -882,8 +877,6 @@ add_executable(test_configfile
buffer.c
array.c
data_config.c
data_integer.c
data_string.c
http_header.c
http_kv.c
vector.c
@ -898,8 +891,6 @@ add_executable(test_keyvalue
buffer.c
base64.c
array.c
data_integer.c
data_string.c
log.c
)
add_test(NAME test_keyvalue COMMAND test_keyvalue)
@ -908,8 +899,6 @@ add_executable(test_mod_access
t/test_mod_access.c
buffer.c
array.c
data_integer.c
data_string.c
log.c
)
add_test(NAME test_mod_access COMMAND test_mod_access)
@ -918,8 +907,6 @@ add_executable(test_mod_evhost
t/test_mod_evhost.c
buffer.c
array.c
data_integer.c
data_string.c
log.c
)
add_test(NAME test_mod_evhost COMMAND test_mod_evhost)
@ -928,8 +915,6 @@ add_executable(test_mod_simple_vhost
t/test_mod_simple_vhost.c
buffer.c
array.c
data_integer.c
data_string.c
log.c
)
add_test(NAME test_mod_simple_vhost COMMAND test_mod_simple_vhost)
@ -938,8 +923,6 @@ add_executable(test_mod_userdir
t/test_mod_userdir.c
buffer.c
array.c
data_integer.c
data_string.c
log.c
)
add_test(NAME test_mod_userdir COMMAND test_mod_userdir)
@ -950,8 +933,6 @@ add_executable(test_request
buffer.c
burl.c
array.c
data_integer.c
data_string.c
http_header.c
http_kv.c
log.c

18
src/Makefile.am

@ -73,8 +73,6 @@ common_src=base64.c buffer.c burl.c log.c \
http_header.c http_kv.c keyvalue.c chunk.c \
http_chunk.c fdevent.c gw_backend.c \
stat_cache.c plugin.c http_etag.c array.c \
data_string.c data_array.c \
data_integer.c \
algo_hmac.c algo_md5.c algo_sha1.c algo_splaytree.c \
fdevent_select.c fdevent_libev.c \
fdevent_poll.c fdevent_linux_sysepoll.c \
@ -626,7 +624,7 @@ lighttpd_LDFLAGS = -export-dynamic
endif
t_test_array_SOURCES = t/test_array.c array.c data_array.c data_integer.c data_string.c buffer.c
t_test_array_SOURCES = t/test_array.c array.c buffer.c
t_test_array_LDADD = $(LIBUNWIND_LIBS)
t_test_buffer_SOURCES = t/test_buffer.c buffer.c
@ -638,25 +636,25 @@ t_test_base64_LDADD = $(LIBUNWIND_LIBS)
t_test_burl_SOURCES = t/test_burl.c burl.c buffer.c base64.c
t_test_burl_LDADD = $(LIBUNWIND_LIBS)
t_test_configfile_SOURCES = t/test_configfile.c buffer.c array.c data_config.c data_integer.c data_string.c http_header.c http_kv.c vector.c log.c sock_addr.c
t_test_configfile_SOURCES = t/test_configfile.c buffer.c array.c data_config.c http_header.c http_kv.c vector.c log.c sock_addr.c
t_test_configfile_LDADD = $(PCRE_LIB) $(LIBUNWIND_LIBS)
t_test_keyvalue_SOURCES = t/test_keyvalue.c burl.c buffer.c base64.c array.c data_integer.c data_string.c log.c
t_test_keyvalue_SOURCES = t/test_keyvalue.c burl.c buffer.c base64.c array.c log.c
t_test_keyvalue_LDADD = $(PCRE_LIB) $(LIBUNWIND_LIBS)
t_test_mod_access_SOURCES = t/test_mod_access.c buffer.c array.c data_integer.c data_string.c log.c
t_test_mod_access_SOURCES = t/test_mod_access.c buffer.c array.c log.c
t_test_mod_access_LDADD = $(LIBUNWIND_LIBS)
t_test_mod_evhost_SOURCES = t/test_mod_evhost.c buffer.c array.c data_integer.c data_string.c log.c
t_test_mod_evhost_SOURCES = t/test_mod_evhost.c buffer.c array.c log.c
t_test_mod_evhost_LDADD = $(LIBUNWIND_LIBS)
t_test_mod_simple_vhost_SOURCES = t/test_mod_simple_vhost.c buffer.c array.c data_integer.c data_string.c log.c
t_test_mod_simple_vhost_SOURCES = t/test_mod_simple_vhost.c buffer.c array.c log.c
t_test_mod_simple_vhost_LDADD = $(LIBUNWIND_LIBS)
t_test_mod_userdir_SOURCES = t/test_mod_userdir.c buffer.c array.c data_integer.c data_string.c log.c
t_test_mod_userdir_SOURCES = t/test_mod_userdir.c buffer.c array.c log.c
t_test_mod_userdir_LDADD = $(LIBUNWIND_LIBS)
t_test_request_SOURCES = t/test_request.c base64.c buffer.c burl.c array.c data_integer.c data_string.c http_header.c http_kv.c log.c sock_addr.c
t_test_request_SOURCES = t/test_request.c base64.c buffer.c burl.c array.c http_header.c http_kv.c log.c sock_addr.c
t_test_request_LDADD = $(LIBUNWIND_LIBS)
noinst_HEADERS = $(hdr)

2
src/SConscript

@ -59,8 +59,6 @@ common_src = Split("base64.c buffer.c burl.c log.c \
http_header.c http_kv.c keyvalue.c chunk.c \
http_chunk.c fdevent.c gw_backend.c \
stat_cache.c plugin.c http_etag.c array.c \
data_string.c data_array.c \
data_integer.c \
algo_hmac.c algo_md5.c algo_sha1.c algo_splaytree.c \
fdevent_select.c fdevent_libev.c \
fdevent_poll.c fdevent_linux_sysepoll.c \

123
src/array.c

@ -7,6 +7,123 @@
#include <stdlib.h>
#include <limits.h>
__attribute_cold__
static data_unset *array_data_string_copy(const data_unset *s) {
data_string *src = (data_string *)s;
data_string *ds = array_data_string_init();
if (!buffer_is_empty(&src->key)) buffer_copy_buffer(&ds->key, &src->key);
buffer_copy_buffer(&ds->value, &src->value);
return (data_unset *)ds;
}
__attribute_cold__
static void array_data_string_insert_dup(data_unset *dst, data_unset *src) {
data_string *ds_dst = (data_string *)dst;
data_string *ds_src = (data_string *)src;
if (!buffer_is_empty(&ds_dst->value))
buffer_append_str2(&ds_dst->value, CONST_STR_LEN(", "),
CONST_BUF_LEN(&ds_src->value));
else
buffer_copy_buffer(&ds_dst->value, &ds_src->value);
src->fn->free(src);
}
static void array_data_string_free(data_unset *du) {
data_string *ds = (data_string *)du;
free(ds->key.ptr);
free(ds->value.ptr);
free(ds);
}
__attribute_noinline__
data_string *array_data_string_init(void) {
static const struct data_methods fn = {
array_data_string_copy,
array_data_string_free,
array_data_string_insert_dup,
};
data_string *ds = calloc(1, sizeof(*ds));
force_assert(NULL != ds);
ds->type = TYPE_STRING;
ds->fn = &fn;
return ds;
}
__attribute_cold__
static data_unset *array_data_integer_copy(const data_unset *s) {
data_integer *src = (data_integer *)s;
data_integer *di = array_data_integer_init();
if (!buffer_is_empty(&src->key)) buffer_copy_buffer(&di->key, &src->key);
di->value = src->value;
return (data_unset *)di;
}
__attribute_cold__
static void array_data_integer_insert_dup(data_unset *dst, data_unset *src) {
UNUSED(dst);
src->fn->free(src);
}
static void array_data_integer_free(data_unset *du) {
data_integer *di = (data_integer *)du;
free(di->key.ptr);
free(di);
}
__attribute_noinline__
data_integer *array_data_integer_init(void) {
static const struct data_methods fn = {
array_data_integer_copy,
array_data_integer_free,
array_data_integer_insert_dup,
};
data_integer *di = calloc(1, sizeof(*di));
force_assert(NULL != di);
di->type = TYPE_INTEGER;
di->fn = &fn;
return di;
}
__attribute_cold__
static data_unset *array_data_array_copy(const data_unset *s) {
data_array *src = (data_array *)s;
data_array *da = array_data_array_init();
if (!buffer_is_empty(&src->key)) buffer_copy_buffer(&da->key, &src->key);
array_copy_array(&da->value, &src->value);
return (data_unset *)da;
}
__attribute_cold__
static void array_data_array_insert_dup(data_unset *dst, data_unset *src) {
UNUSED(dst);
src->fn->free(src);
}
static void array_data_array_free(data_unset *du) {
data_array *da = (data_array *)du;
free(da->key.ptr);
array_free_data(&da->value);
free(da);
}
__attribute_noinline__
data_array *array_data_array_init(void) {
static const struct data_methods fn = {
array_data_array_copy,
array_data_array_free,
array_data_array_insert_dup,
};
data_array *da = calloc(1, sizeof(*da));
force_assert(NULL != da);
da->type = TYPE_ARRAY;
da->fn = &fn;
return da;
}
__attribute_cold__
static void array_extend(array * const a, uint32_t n) {
/* This data structure should not be used for nearly so many entries */
@ -273,9 +390,9 @@ static void array_insert_data_at_pos(array * const a, data_unset * const entry,
static data_integer * array_insert_integer_at_pos(array * const a, const uint32_t pos) {
#if 0 /*(not currently used by lighttpd in way that reuse would occur)*/
data_integer *di = (data_integer *)array_get_unused_element(a,TYPE_INTEGER);
if (NULL == di) di = data_integer_init();
if (NULL == di) di = array_data_integer_init();
#else
data_integer * const di = data_integer_init();
data_integer * const di = array_data_integer_init();
#endif
array_insert_data_at_pos(a, (data_unset *)di, pos);
return di;
@ -284,7 +401,7 @@ static data_integer * array_insert_integer_at_pos(array * const a, const uint32_
__attribute_hot__
static data_string * array_insert_string_at_pos(array * const a, const uint32_t pos) {
data_string *ds = (data_string *)array_get_unused_element(a, TYPE_STRING);
if (NULL == ds) ds = data_string_init();
if (NULL == ds) ds = array_data_string_init();
array_insert_data_at_pos(a, (data_unset *)ds, pos);
return ds;
}

8
src/array.h

@ -9,7 +9,7 @@ struct data_unset; /* declaration */
struct data_methods {
struct data_unset *(*copy)(const struct data_unset *src); \
void (*free)(struct data_unset *p); \
int (*insert_dup)(struct data_unset *dst, struct data_unset *src); \
void (*insert_dup)(struct data_unset *dst, struct data_unset *src);
};
typedef enum { TYPE_STRING, TYPE_ARRAY, TYPE_INTEGER, TYPE_CONFIG, TYPE_OTHER } data_type_t;
@ -37,7 +37,7 @@ typedef struct {
} data_string;
__attribute_returns_nonnull__
data_string *data_string_init(void);
data_string *array_data_string_init(void);
typedef struct {
DATA_UNSET;
@ -46,7 +46,7 @@ typedef struct {
} data_array;
__attribute_returns_nonnull__
data_array *data_array_init(void);
data_array *array_data_array_init(void);
typedef struct {
DATA_UNSET;
@ -55,7 +55,7 @@ typedef struct {
} data_integer;
__attribute_returns_nonnull__
data_integer *data_integer_init(void);
data_integer *array_data_integer_init(void);
__attribute_returns_nonnull__
array *array_init(uint32_t n);

14
src/configparser.y

@ -77,7 +77,7 @@ static data_unset *configparser_merge_data(data_unset *op1, const data_unset *op
buffer_append_int(&ds->value, ((data_integer*)op2)->value);
return op1;
} else if (op1->type == TYPE_INTEGER && op2->type == TYPE_STRING) {
data_string *ds = data_string_init();
data_string *ds = array_data_string_init();
buffer_append_int(&ds->value, ((data_integer*)op1)->value);
buffer_append_string_buffer(&ds->value, &((data_string*)op2)->value);
op1->fn->free(op1);
@ -497,7 +497,7 @@ value(A) ::= key(B). {
if (NULL != (env = getenv(B->ptr + 4))) {
data_string *ds;
ds = data_string_init();
ds = array_data_string_init();
buffer_append_string(&ds->value, env);
A = (data_unset *)ds;
}
@ -515,8 +515,8 @@ value(A) ::= key(B). {
}
value(A) ::= STRING(B). {
A = (data_unset *)data_string_init();
/* assumes data_string_init() result does not require swap and buffer_free()*/
A = (data_unset *)array_data_string_init();
/* assumes array_data_string_init() result does not need swap, buffer_free()*/
memcpy(&((data_string *)A)->value, B, sizeof(*B));
free(B);
B = NULL;
@ -524,7 +524,7 @@ value(A) ::= STRING(B). {
value(A) ::= INTEGER(B). {
char *endptr;
A = (data_unset *)data_integer_init();
A = (data_unset *)array_data_integer_init();
errno = 0;
((data_integer *)(A))->value = strtol(B->ptr, &endptr, 10);
/* skip trailing whitespace */
@ -537,8 +537,8 @@ value(A) ::= INTEGER(B). {
B = NULL;
}
value(A) ::= array(B). {
A = (data_unset *)data_array_init();
/* assumes data_array_init() result does not require swap and array_free() */
A = (data_unset *)array_data_array_init();
/* assumes array_data_array_init() result does not need swap, array_free() */
memcpy(&((data_array *)(A))->value, B, sizeof(*B));
free(B);
B = NULL;

51
src/data_array.c

@ -1,51 +0,0 @@
#include "first.h"
#include "array.h"
#include "buffer.h"
#include <stdlib.h>
__attribute_cold__
static data_unset *data_array_copy(const data_unset *s) {
data_array *src = (data_array *)s;
data_array *ds = data_array_init();
if (!buffer_is_empty(&src->key)) buffer_copy_buffer(&ds->key, &src->key);
array_copy_array(&ds->value, &src->value);
return (data_unset *)ds;
}
static void data_array_free(data_unset *d) {
data_array *ds = (data_array *)d;
free(ds->key.ptr);
array_free_data(&ds->value);
free(d);
}
__attribute_cold__
static int data_array_insert_dup(data_unset *dst, data_unset *src) {
UNUSED(dst);
src->fn->free(src);
return 0;
}
data_array *data_array_init(void) {
static const struct data_methods fn = {
data_array_copy,
data_array_free,
data_array_insert_dup,
};
data_array *ds;
ds = calloc(1, sizeof(*ds));
force_assert(NULL != ds);
ds->type = TYPE_ARRAY;
ds->fn = &fn;
return ds;
}

5
src/data_config.c

@ -49,12 +49,9 @@ static void data_config_free(data_unset *d) {
}
__attribute_cold__
static int data_config_insert_dup(data_unset *dst, data_unset *src) {
static void data_config_insert_dup(data_unset *dst, data_unset *src) {
UNUSED(dst);
src->fn->free(src);
return 0;
}
data_config *data_config_init(void) {

52
src/data_integer.c

@ -1,52 +0,0 @@
#include "first.h"
#include "array.h"
#include "buffer.h"
#include <stdlib.h>
__attribute_cold__
static data_unset *data_integer_copy(const data_unset *s) {
data_integer *src = (data_integer *)s;
data_integer *ds = data_integer_init();
if (!buffer_is_empty(&src->key)) buffer_copy_buffer(&ds->key, &src->key);
ds->value = src->value;
return (data_unset *)ds;
}
static void data_integer_free(data_unset *d) {
data_integer *ds = (data_integer *)d;
free(ds->key.ptr);
free(d);
}
__attribute_cold__
static int data_integer_insert_dup(data_unset *dst, data_unset *src) {
UNUSED(dst);
src->fn->free(src);
return 0;
}
data_integer *data_integer_init(void) {
static const struct data_methods fn = {
data_integer_copy,
data_integer_free,
data_integer_insert_dup,
};
data_integer *ds;
ds = calloc(1, sizeof(*ds));
force_assert(NULL != ds);
ds->value = 0;
ds->type = TYPE_INTEGER;
ds->fn = &fn;
return ds;
}

58
src/data_string.c

@ -1,58 +0,0 @@
#include "first.h"
#include "array.h"
#include "buffer.h"
#include <stdlib.h>
__attribute_cold__
static data_unset *data_string_copy(const data_unset *s) {
data_string *src = (data_string *)s;
data_string *ds = data_string_init();
if (!buffer_is_empty(&src->key)) buffer_copy_buffer(&ds->key, &src->key);
buffer_copy_buffer(&ds->value, &src->value);
return (data_unset *)ds;
}
static void data_string_free(data_unset *d) {
data_string *ds = (data_string *)d;
free(ds->key.ptr);
free(ds->value.ptr);
free(d);
}
__attribute_cold__
static int data_string_insert_dup(data_unset *dst, data_unset *src) {
data_string *ds_dst = (data_string *)dst;
data_string *ds_src = (data_string *)src;
if (!buffer_is_empty(&ds_dst->value))
buffer_append_str2(&ds_dst->value, CONST_STR_LEN(", "),
CONST_BUF_LEN(&ds_src->value));
else
buffer_copy_buffer(&ds_dst->value, &ds_src->value);
src->fn->free(src);
return 0;
}
data_string *data_string_init(void) {
static const struct data_methods fn = {
data_string_copy,
data_string_free,
data_string_insert_dup,
};
data_string *ds;
ds = calloc(1, sizeof(*ds));
force_assert(NULL != ds);
ds->type = TYPE_STRING;
ds->fn = &fn;
return ds;
}

19
src/meson.build

@ -716,9 +716,6 @@ common_src = [
'chunk.c',
'configfile-glue.c',
'connections-glue.c',
'data_array.c',
'data_integer.c',
'data_string.c',
'http_etag.c',
'fdevent_freebsd_kqueue.c',
'fdevent_libev.c',
@ -864,7 +861,7 @@ executable('lighttpd', configparser,
)
test('test_array', executable('test_array',
sources: ['t/test_array.c', 'array.c', 'data_array.c', 'data_integer.c', 'data_string.c', 'buffer.c'],
sources: ['t/test_array.c', 'array.c', 'buffer.c'],
dependencies: common_flags + libunwind,
build_by_default: false,
))
@ -893,8 +890,6 @@ test('test_configfile', executable('test_configfile',
'buffer.c',
'array.c',
'data_config.c',
'data_integer.c',
'data_string.c',
'http_header.c',
'http_kv.c',
'vector.c',
@ -912,8 +907,6 @@ test('test_keyvalue', executable('test_keyvalue',
'buffer.c',
'base64.c',
'array.c',
'data_integer.c',
'data_string.c',
'log.c',
],
dependencies: common_flags + libpcre + libunwind,
@ -925,8 +918,6 @@ test('test_mod_access', executable('test_mod_access',
't/test_mod_access.c',
'buffer.c',
'array.c',
'data_integer.c',
'data_string.c',
'log.c',
],
dependencies: common_flags + libunwind,
@ -938,8 +929,6 @@ test('test_mod_evhost', executable('test_mod_evhost',
't/test_mod_evhost.c',
'buffer.c',
'array.c',
'data_integer.c',
'data_string.c',
'log.c',
],
dependencies: common_flags + libunwind,
@ -951,8 +940,6 @@ test('test_mod_simple_vhost', executable('test_mod_simple_vhost',
't/test_mod_simple_vhost.c',
'buffer.c',
'array.c',
'data_integer.c',
'data_string.c',
'log.c',
],
dependencies: common_flags + libunwind,
@ -964,8 +951,6 @@ test('test_mod_userdir', executable('test_mod_userdir',
't/test_mod_userdir.c',
'buffer.c',
'array.c',
'data_integer.c',
'data_string.c',
'log.c',
],
dependencies: common_flags + libunwind,
@ -979,8 +964,6 @@ test('test_request', executable('test_request',
'buffer.c',
'burl.c',
'array.c',
'data_integer.c',
'data_string.c',
'http_header.c',
'http_kv.c',
'log.c',

Loading…
Cancel
Save