diff options
author | Timo Teräs <timo.teras@iki.fi> | 2020-05-19 11:39:21 +0300 |
---|---|---|
committer | Timo Teräs <timo.teras@iki.fi> | 2020-05-19 12:02:56 +0300 |
commit | d0edeec8fb8fa5abee8b3065cea5e4882d0c51c4 (patch) | |
tree | 10dbc6d72c9cef13a26cb539df43e92e4bf78d74 /src/blob.c | |
parent | 12fdf6fc219321d22825042ae08efd322acc0310 (diff) | |
download | apk-tools-d0edeec8fb8fa5abee8b3065cea5e4882d0c51c4.tar.gz apk-tools-d0edeec8fb8fa5abee8b3065cea5e4882d0c51c4.tar.bz2 apk-tools-d0edeec8fb8fa5abee8b3065cea5e4882d0c51c4.tar.xz apk-tools-d0edeec8fb8fa5abee8b3065cea5e4882d0c51c4.zip |
make the atom functions not use global state
This greatly helps with memory management on applications that
may want to daemonize and open/close database several times.
Also the lifetime and "owner" of memory for all data is now
explicitly bound to owning struct apk_database, which might
be helpful when writing language bindings. As side effect, the
interned "atoms" are unique only within what apk_database, so
comparing packages from different apk_database may not work
as expected.
Fixes #10697
Diffstat (limited to 'src/blob.c')
-rw-r--r-- | src/blob.c | 76 |
1 files changed, 0 insertions, 76 deletions
@@ -15,11 +15,6 @@ #include "apk_blob.h" #include "apk_hash.h" -struct apk_blob_atom { - struct hlist_node hash_node; - apk_blob_t blob; -}; - char *apk_blob_cstr(apk_blob_t blob) { char *cstr; @@ -640,77 +635,6 @@ err: *b = APK_BLOB_NULL; } -static apk_blob_t atom_hash_get_key(apk_hash_item item) -{ - return ((struct apk_blob_atom *) item)->blob; -} - -struct apk_hash atom_hash; -static struct apk_hash_ops atom_ops = { - .node_offset = offsetof(struct apk_blob_atom, hash_node), - .get_key = atom_hash_get_key, - .hash_key = apk_blob_hash, - .compare = apk_blob_compare, - .delete_item = (apk_hash_delete_f) free, -}; - -apk_blob_t apk_null_blob = {0,0}; - -#ifdef VALGRIND -static void apk_atom_fini(void) -{ - apk_hash_free(&atom_hash); -} -#endif - -void apk_atom_init(void) -{ -#ifdef VALGRIND - atexit(apk_atom_fini); -#endif - apk_hash_init(&atom_hash, &atom_ops, 10000); -} - -apk_blob_t *apk_blob_atomize(apk_blob_t blob) -{ - struct apk_blob_atom *atom; - unsigned long hash = apk_hash_from_key(&atom_hash, blob); - - if (blob.len < 0 || blob.ptr == NULL) - return &apk_null_blob; - - atom = (struct apk_blob_atom *) apk_hash_get_hashed(&atom_hash, blob, hash); - if (atom != NULL) - return &atom->blob; - - atom = malloc(sizeof(*atom)); - atom->blob = blob; - apk_hash_insert_hashed(&atom_hash, atom, hash); - - return &atom->blob; -} - -apk_blob_t *apk_blob_atomize_dup(apk_blob_t blob) -{ - struct apk_blob_atom *atom; - unsigned long hash = apk_hash_from_key(&atom_hash, blob); - char *ptr; - - if (blob.len < 0 || blob.ptr == NULL) - return &apk_null_blob; - atom = (struct apk_blob_atom *) apk_hash_get_hashed(&atom_hash, blob, hash); - if (atom != NULL) - return &atom->blob; - - atom = malloc(sizeof(*atom) + blob.len); - ptr = (char*) (atom + 1); - memcpy(ptr, blob.ptr, blob.len); - atom->blob = APK_BLOB_PTR_LEN(ptr, blob.len); - apk_hash_insert_hashed(&atom_hash, atom, hash); - - return &atom->blob; -} - #if defined(__GLIBC__) && !defined(__UCLIBC__) size_t strlcpy(char *dst, const char *src, size_t size) { |