diff options
author | Vicent Marti <tanoku@gmail.com> | 2015-10-27 17:26:04 +0100 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2015-10-28 10:13:13 +0100 |
commit | 1e5e02b4f47779fe3733b1a6ab24a6ca13099ec3 (patch) | |
tree | ac854971d3609c9065cbaee0119541428bdc9477 /src | |
parent | efc659b071e02c6d3b39b32b35ab83233d978956 (diff) | |
download | libgit2-1e5e02b4f47779fe3733b1a6ab24a6ca13099ec3.tar.gz |
pool: Simplify implementation
Diffstat (limited to 'src')
-rw-r--r-- | src/attr_file.c | 6 | ||||
-rw-r--r-- | src/attrcache.c | 5 | ||||
-rw-r--r-- | src/checkout.c | 7 | ||||
-rw-r--r-- | src/diff.c | 5 | ||||
-rw-r--r-- | src/diff_tform.c | 6 | ||||
-rw-r--r-- | src/index.c | 2 | ||||
-rw-r--r-- | src/iterator.c | 7 | ||||
-rw-r--r-- | src/merge.c | 6 | ||||
-rw-r--r-- | src/pack-objects.c | 3 | ||||
-rw-r--r-- | src/pathspec.c | 12 | ||||
-rw-r--r-- | src/pool.c | 222 | ||||
-rw-r--r-- | src/pool.h | 37 | ||||
-rw-r--r-- | src/refdb_fs.c | 5 | ||||
-rw-r--r-- | src/revwalk.c | 6 | ||||
-rw-r--r-- | src/sortedcache.c | 5 | ||||
-rw-r--r-- | src/transaction.c | 3 |
16 files changed, 72 insertions, 265 deletions
diff --git a/src/attr_file.c b/src/attr_file.c index 89706865a..500c99bd9 100644 --- a/src/attr_file.c +++ b/src/attr_file.c @@ -35,11 +35,7 @@ int git_attr_file__new( return -1; } - if (git_pool_init(&attrs->pool, 1, 0) < 0) { - attr_file_free(attrs); - return -1; - } - + git_pool_init(&attrs->pool, 1); GIT_REFCOUNT_INC(attrs); attrs->entry = entry; attrs->source = source; diff --git a/src/attrcache.c b/src/attrcache.c index 5bc260460..a57110684 100644 --- a/src/attrcache.c +++ b/src/attrcache.c @@ -388,10 +388,11 @@ int git_attr_cache__do_init(git_repository *repo) * hashtable for attribute macros, and string pool */ if ((ret = git_strmap_alloc(&cache->files)) < 0 || - (ret = git_strmap_alloc(&cache->macros)) < 0 || - (ret = git_pool_init(&cache->pool, 1, 0)) < 0) + (ret = git_strmap_alloc(&cache->macros)) < 0) goto cancel; + git_pool_init(&cache->pool, 1); + cache = git__compare_and_swap(&repo->attrcache, NULL, cache); if (cache) goto cancel; /* raced with another thread, free this but no error */ diff --git a/src/checkout.c b/src/checkout.c index 632556622..d09357f2a 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -1255,11 +1255,13 @@ static int checkout_get_actions( int error = 0, act; const git_index_entry *wditem; git_vector pathspec = GIT_VECTOR_INIT, *deltas; - git_pool pathpool = GIT_POOL_INIT_STRINGPOOL; + git_pool pathpool; git_diff_delta *delta; size_t i, *counts = NULL; uint32_t *actions = NULL; + git_pool_init(&pathpool, 1); + if (data->opts.paths.count > 0 && git_pathspec__vinit(&pathspec, &data->opts.paths, &pathpool) < 0) return -1; @@ -2439,10 +2441,11 @@ static int checkout_data_init( git_config_entry_free(conflict_style); } + git_pool_init(&data->pool, 1); + if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 || (error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 || (error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 || - (error = git_pool_init(&data->pool, 1, 0)) < 0 || (error = git_buf_puts(&data->path, data->opts.target_directory)) < 0 || (error = git_path_to_dir(&data->path)) < 0 || (error = git_strmap_alloc(&data->mkdir_map)) < 0) diff --git a/src/diff.c b/src/diff.c index d98a28966..b5e9b6cd5 100644 --- a/src/diff.c +++ b/src/diff.c @@ -430,8 +430,9 @@ static git_diff *diff_list_alloc( diff->new_src = new_iter->type; memcpy(&diff->opts, &dflt, sizeof(diff->opts)); - if (git_vector_init(&diff->deltas, 0, git_diff_delta__cmp) < 0 || - git_pool_init(&diff->pool, 1, 0) < 0) { + git_pool_init(&diff->pool, 1); + + if (git_vector_init(&diff->deltas, 0, git_diff_delta__cmp) < 0) { git_diff_free(diff); return NULL; } diff --git a/src/diff_tform.c b/src/diff_tform.c index 92647e330..7cff34159 100644 --- a/src/diff_tform.c +++ b/src/diff_tform.c @@ -134,11 +134,11 @@ int git_diff__merge( return -1; } - if (git_vector_init( - &onto_new, onto->deltas.length, git_diff_delta__cmp) < 0 || - git_pool_init(&onto_pool, 1, 0) < 0) + if (git_vector_init(&onto_new, onto->deltas.length, git_diff_delta__cmp) < 0) return -1; + git_pool_init(&onto_pool, 1); + for (i = 0, j = 0; i < onto->deltas.length || j < from->deltas.length; ) { git_diff_delta *o = GIT_VECTOR_GET(&onto->deltas, i); const git_diff_delta *f = GIT_VECTOR_GET(&from->deltas, j); diff --git a/src/index.c b/src/index.c index d9e713899..0adc7e157 100644 --- a/src/index.c +++ b/src/index.c @@ -439,7 +439,7 @@ int git_index_open(git_index **index_out, const char *index_path) return -1; } - git_pool_init(&index->tree_pool, 1, 0); + git_pool_init(&index->tree_pool, 1); if (index_path != NULL) { index->index_file_path = git__strdup(index_path); diff --git a/src/iterator.c b/src/iterator.c index e3a2abf66..ee348de6e 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -567,7 +567,7 @@ static bool tree_iterator__pop_frame(tree_iterator *ti, bool final) tree_iterator__move_to_next(ti, tf); if (!final) { /* if final, don't bother to clean up */ - git_pool_free_array(&ti->pool, tf->n_entries, (void **)tf->entries); + // TODO: maybe free the pool so far? git_buf_rtruncate_at_char(&ti->path, '/'); } @@ -822,8 +822,9 @@ int git_iterator_for_tree( if ((error = iterator__update_ignore_case((git_iterator *)ti, options ? options->flags : 0)) < 0) goto fail; - if ((error = git_pool_init(&ti->pool, sizeof(tree_iterator_entry),0)) < 0 || - (error = tree_iterator__create_root_frame(ti, tree)) < 0 || + git_pool_init(&ti->pool, sizeof(tree_iterator_entry)); + + if ((error = tree_iterator__create_root_frame(ti, tree)) < 0 || (error = tree_iterator__push_frame(ti)) < 0) /* expand root now */ goto fail; diff --git a/src/merge.c b/src/merge.c index 186c77037..e84b1d3e3 100644 --- a/src/merge.c +++ b/src/merge.c @@ -1307,7 +1307,6 @@ GIT_INLINE(int) index_entry_dup_pool( { if (src != NULL) { memcpy(out, src, sizeof(git_index_entry)); - if ((out->path = git_pool_strdup(pool, src->path)) == NULL) return -1; } @@ -1442,10 +1441,11 @@ git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo) diff_list->repo = repo; + git_pool_init(&diff_list->pool, 1); + if (git_vector_init(&diff_list->staged, 0, NULL) < 0 || git_vector_init(&diff_list->conflicts, 0, NULL) < 0 || - git_vector_init(&diff_list->resolved, 0, NULL) < 0 || - git_pool_init(&diff_list->pool, 1, 0) < 0) { + git_vector_init(&diff_list->resolved, 0, NULL) < 0) { git_merge_diff_list__free(diff_list); return NULL; } diff --git a/src/pack-objects.c b/src/pack-objects.c index c4c061a3a..fd181fc5e 100644 --- a/src/pack-objects.c +++ b/src/pack-objects.c @@ -135,8 +135,7 @@ int git_packbuilder_new(git_packbuilder **out, git_repository *repo) if (!pb->walk_objects) goto on_error; - if (git_pool_init(&pb->object_pool, sizeof(git_walk_object), 0) < 0) - goto on_error; + git_pool_init(&pb->object_pool, sizeof(git_walk_object)); pb->repo = repo; pb->nr_threads = 1; /* do not spawn any thread by default */ diff --git a/src/pathspec.c b/src/pathspec.c index 9304da705..5bb69ec4b 100644 --- a/src/pathspec.c +++ b/src/pathspec.c @@ -237,9 +237,9 @@ int git_pathspec__init(git_pathspec *ps, const git_strarray *paths) memset(ps, 0, sizeof(*ps)); ps->prefix = git_pathspec_prefix(paths); + git_pool_init(&ps->pool, 1); - if ((error = git_pool_init(&ps->pool, 1, 0)) < 0 || - (error = git_pathspec__vinit(&ps->pathspec, paths, &ps->pool)) < 0) + if ((error = git_pathspec__vinit(&ps->pathspec, paths, &ps->pool)) < 0) git_pathspec__clear(ps); return error; @@ -312,15 +312,11 @@ static git_pathspec_match_list *pathspec_match_alloc( git_pathspec *ps, int datatype) { git_pathspec_match_list *m = git__calloc(1, sizeof(git_pathspec_match_list)); - - if (m != NULL && git_pool_init(&m->pool, 1, 0) < 0) { - pathspec_match_free(m); - m = NULL; - } - if (!m) return NULL; + git_pool_init(&m->pool, 1); + /* need to keep reference to pathspec and increment refcount because * failures array stores pointers to the pattern strings of the * pathspec that had no matches diff --git a/src/pool.c b/src/pool.c index c93d78182..78277eb40 100644 --- a/src/pool.c +++ b/src/pool.c @@ -8,67 +8,49 @@ struct git_pool_page { git_pool_page *next; uint32_t size; uint32_t avail; - GIT_ALIGN(char data[GIT_FLEX_ARRAY], 8); + char data[GIT_FLEX_ARRAY]; }; -struct pool_freelist { - struct pool_freelist *next; -}; +static void *pool_alloc_page(git_pool *pool, uint32_t size); -#define GIT_POOL_MIN_USABLE 4 -#define GIT_POOL_MIN_PAGESZ 2 * sizeof(void*) +uint32_t git_pool__system_page_size(void) +{ + static uint32_t size = 0; -static void *pool_alloc_page(git_pool *pool, uint32_t size); -static void pool_insert_page(git_pool *pool, git_pool_page *page); + if (!size) { + size_t page_size; + if (git__page_size(&page_size) < 0) + page_size = 4096; + size = page_size - 2 * sizeof(void *); /* allow space for malloc overhead */ + } -int git_pool_init( - git_pool *pool, uint32_t item_size, uint32_t items_per_page) + return size; +} + +void git_pool_init(git_pool *pool, uint32_t item_size) { + const uint32_t align_size = sizeof(void *) - 1; assert(pool); - if (!item_size) - item_size = 1; - /* round up item_size for decent object alignment */ - if (item_size > 4) - item_size = (item_size + 7) & ~7; - else if (item_size == 3) - item_size = 4; - - if (!items_per_page) - items_per_page = git_pool__suggest_items_per_page(item_size); - if (item_size * items_per_page < GIT_POOL_MIN_PAGESZ) - items_per_page = (GIT_POOL_MIN_PAGESZ + item_size - 1) / item_size; + if (item_size > 1) + item_size = (item_size + align_size) & ~align_size; memset(pool, 0, sizeof(git_pool)); pool->item_size = item_size; - pool->page_size = item_size * items_per_page; - - return 0; + pool->page_size = git_pool__system_page_size(); } void git_pool_clear(git_pool *pool) { git_pool_page *scan, *next; - for (scan = pool->open; scan != NULL; scan = next) { + for (scan = pool->pages; scan != NULL; scan = next) { next = scan->next; git__free(scan); } - pool->open = NULL; - - for (scan = pool->full; scan != NULL; scan = next) { - next = scan->next; - git__free(scan); - } - pool->full = NULL; - - pool->free_list = NULL; + pool->pages = NULL; pool->items = 0; - - pool->has_string_alloc = 0; - pool->has_multi_item_alloc = 0; - pool->has_large_page_alloc = 0; } void git_pool_swap(git_pool *a, git_pool *b) @@ -83,110 +65,40 @@ void git_pool_swap(git_pool *a, git_pool *b) memcpy(b, &temp, sizeof(temp)); } -static void pool_insert_page(git_pool *pool, git_pool_page *page) -{ - git_pool_page *scan; - - /* If there are no open pages or this page has the most open space, - * insert it at the beginning of the list. This is the common case. - */ - if (pool->open == NULL || pool->open->avail < page->avail) { - page->next = pool->open; - pool->open = page; - return; - } - - /* Otherwise insert into sorted position. */ - for (scan = pool->open; - scan->next && scan->next->avail > page->avail; - scan = scan->next); - page->next = scan->next; - scan->next = page; -} - static void *pool_alloc_page(git_pool *pool, uint32_t size) { git_pool_page *page; - uint32_t new_page_size; + const uint32_t new_page_size = (size <= pool->page_size) ? pool->page_size : size; size_t alloc_size; - if (size <= pool->page_size) - new_page_size = pool->page_size; - else { - new_page_size = size; - pool->has_large_page_alloc = 1; - } - if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, new_page_size, sizeof(git_pool_page)) || !(page = git__calloc(1, alloc_size))) return NULL; - page->size = new_page_size; + page->size = new_page_size; page->avail = new_page_size - size; + page->next = pool->pages; - if (page->avail > 0) - pool_insert_page(pool, page); - else { - page->next = pool->full; - pool->full = page; - } - + pool->pages = page; pool->items++; return page->data; } -GIT_INLINE(void) pool_remove_page( - git_pool *pool, git_pool_page *page, git_pool_page *prev) -{ - if (prev == NULL) - pool->open = page->next; - else - prev->next = page->next; -} - void *git_pool_malloc(git_pool *pool, uint32_t items) { - git_pool_page *scan = pool->open, *prev; - uint32_t size = ((items * pool->item_size) + 7) & ~7; - void *ptr = NULL; + const uint32_t size = items * pool->item_size; - pool->has_string_alloc = 0; - if (items > 1) - pool->has_multi_item_alloc = 1; - else if (pool->free_list != NULL) { - ptr = pool->free_list; - pool->free_list = ((struct pool_freelist *)pool->free_list)->next; - return ptr; - } + git_pool_page *page = pool->pages; + void *ptr = NULL; - /* just add a block if there is no open one to accommodate this */ - if (size >= pool->page_size || !scan || scan->avail < size) + if (!page || page->avail < size) return pool_alloc_page(pool, size); + ptr = &page->data[page->size - page->avail]; + page->avail -= size; pool->items++; - /* find smallest block in free list with space */ - for (scan = pool->open, prev = NULL; - scan->next && scan->next->avail >= size; - prev = scan, scan = scan->next); - - /* allocate space from the block */ - ptr = &scan->data[scan->size - scan->avail]; - scan->avail -= size; - - /* move to full list if there is almost no space left */ - if (scan->avail < pool->item_size || scan->avail < GIT_POOL_MIN_USABLE) { - pool_remove_page(pool, scan, prev); - scan->next = pool->full; - pool->full = scan; - } - /* reorder list if block is now smaller than the one after it */ - else if (scan->next != NULL && scan->next->avail > scan->avail) { - pool_remove_page(pool, scan, prev); - pool_insert_page(pool, scan); - } - return ptr; } @@ -204,15 +116,12 @@ char *git_pool_strndup(git_pool *pool, const char *str, size_t n) ptr[n] = '\0'; } - pool->has_string_alloc = 1; - return ptr; } char *git_pool_strdup(git_pool *pool, const char *str) { assert(pool && str && pool->item_size == sizeof(char)); - return git_pool_strndup(pool, str, strlen(str)); } @@ -238,88 +147,23 @@ char *git_pool_strcat(git_pool *pool, const char *a, const char *b) memcpy(((char *)ptr) + len_a, b, len_b); *(((char *)ptr) + len_a + len_b) = '\0'; } - pool->has_string_alloc = 1; - return ptr; } -void git_pool_free(git_pool *pool, void *ptr) -{ - struct pool_freelist *item = ptr; - - assert(pool && pool->item_size >= sizeof(void*)); - - if (item) { - item->next = pool->free_list; - pool->free_list = item; - } -} - -void git_pool_free_array(git_pool *pool, size_t count, void **ptrs) -{ - struct pool_freelist **items = (struct pool_freelist **)ptrs; - size_t i; - - assert(pool && ptrs && pool->item_size >= sizeof(void*)); - - if (!count) - return; - - for (i = count - 1; i > 0; --i) - items[i]->next = items[i - 1]; - - items[i]->next = pool->free_list; - pool->free_list = items[count - 1]; -} - uint32_t git_pool__open_pages(git_pool *pool) { uint32_t ct = 0; git_pool_page *scan; - for (scan = pool->open; scan != NULL; scan = scan->next) ct++; - return ct; -} - -uint32_t git_pool__full_pages(git_pool *pool) -{ - uint32_t ct = 0; - git_pool_page *scan; - for (scan = pool->full; scan != NULL; scan = scan->next) ct++; + for (scan = pool->pages; scan != NULL; scan = scan->next) ct++; return ct; } bool git_pool__ptr_in_pool(git_pool *pool, void *ptr) { git_pool_page *scan; - for (scan = pool->open; scan != NULL; scan = scan->next) - if ((void *)scan->data <= ptr && - (void *)(((char *)scan->data) + scan->size) > ptr) - return true; - for (scan = pool->full; scan != NULL; scan = scan->next) + for (scan = pool->pages; scan != NULL; scan = scan->next) if ((void *)scan->data <= ptr && (void *)(((char *)scan->data) + scan->size) > ptr) return true; return false; } - -uint32_t git_pool__system_page_size(void) -{ - static uint32_t size = 0; - - if (!size) { - size_t page_size; - if (git__page_size(&page_size) < 0) - page_size = 4096; - size = page_size - 2 * sizeof(void *); /* allow space for malloc overhead */ - } - - return size; -} - -uint32_t git_pool__suggest_items_per_page(uint32_t item_size) -{ - uint32_t page_bytes = - git_pool__system_page_size() - sizeof(git_pool_page); - return page_bytes / item_size; -} - diff --git a/src/pool.h b/src/pool.h index b0007f315..ef71d4eb6 100644 --- a/src/pool.h +++ b/src/pool.h @@ -28,19 +28,12 @@ typedef struct git_pool_page git_pool_page; * For examples of how to set up a `git_pool` see `git_pool_init`. */ typedef struct { - git_pool_page *open; /* pages with space left */ - git_pool_page *full; /* pages with no space left */ - void *free_list; /* optional: list of freed blocks */ + git_pool_page *pages; /* pages with space left */ uint32_t item_size; /* size of single alloc unit in bytes */ uint32_t page_size; /* size of page in bytes */ uint32_t items; - unsigned has_string_alloc : 1; /* was the strdup function used */ - unsigned has_multi_item_alloc : 1; /* was items ever > 1 in malloc */ - unsigned has_large_page_alloc : 1; /* are any pages > page_size */ } git_pool; -#define GIT_POOL_INIT_STRINGPOOL { 0, 0, 0, 1, 4000, 0, 0, 0, 0 } - /** * Initialize a pool. * @@ -57,8 +50,7 @@ typedef struct { * Of course, you can use this in other ways, but those are the * two most common patterns. */ -extern int git_pool_init( - git_pool *pool, uint32_t item_size, uint32_t items_per_page); +extern void git_pool_init(git_pool *pool, uint32_t item_size); /** * Free all items in pool @@ -114,35 +106,10 @@ extern char *git_pool_strdup_safe(git_pool *pool, const char *str); */ extern char *git_pool_strcat(git_pool *pool, const char *a, const char *b); -/** - * Push a block back onto the free list for the pool. - * - * This is allowed only if the item_size is >= sizeof(void*). - * - * In some cases, it is helpful to "release" an allocated block - * for reuse. Pools don't support a general purpose free, but - * they will keep a simple free blocks linked list provided the - * native block size is large enough to hold a void pointer - */ -extern void git_pool_free(git_pool *pool, void *ptr); - -/** - * Push an array of pool allocated blocks efficiently onto the free list. - * - * This has the same constraints as `git_pool_free()` above. - */ -extern void git_pool_free_array(git_pool *pool, size_t count, void **ptrs); - /* * Misc utilities */ - extern uint32_t git_pool__open_pages(git_pool *pool); - -extern uint32_t git_pool__full_pages(git_pool *pool); - extern bool git_pool__ptr_in_pool(git_pool *pool, void *ptr); -extern uint32_t git_pool__suggest_items_per_page(uint32_t item_size); - #endif diff --git a/src/refdb_fs.c b/src/refdb_fs.c index 921f7862b..af96821df 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -626,8 +626,9 @@ static int refdb_fs_backend__iterator( iter = git__calloc(1, sizeof(refdb_fs_iter)); GITERR_CHECK_ALLOC(iter); - if (git_pool_init(&iter->pool, 1, 0) < 0 || - git_vector_init(&iter->loose, 8, NULL) < 0) + git_pool_init(&iter->pool, 1); + + if (git_vector_init(&iter->loose, 8, NULL) < 0) goto fail; if (glob != NULL && diff --git a/src/revwalk.c b/src/revwalk.c index dcdd97915..89279ed1f 100644 --- a/src/revwalk.c +++ b/src/revwalk.c @@ -535,12 +535,10 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo) walk->commits = git_oidmap_alloc(); GITERR_CHECK_ALLOC(walk->commits); - if (git_pqueue_init( - &walk->iterator_time, 0, 8, git_commit_list_time_cmp) < 0 || - git_pool_init(&walk->commit_pool, 1, - git_pool__suggest_items_per_page(COMMIT_ALLOC) * COMMIT_ALLOC) < 0) + if (git_pqueue_init(&walk->iterator_time, 0, 8, git_commit_list_time_cmp) < 0) return -1; + git_pool_init(&walk->commit_pool, COMMIT_ALLOC); walk->get_next = &revwalk_next_unsorted; walk->enqueue = &revwalk_enqueue_unsorted; diff --git a/src/sortedcache.c b/src/sortedcache.c index 115175724..5c2a167a7 100644 --- a/src/sortedcache.c +++ b/src/sortedcache.c @@ -20,8 +20,9 @@ int git_sortedcache_new( sc = git__calloc(1, alloclen); GITERR_CHECK_ALLOC(sc); - if (git_pool_init(&sc->pool, 1, 0) < 0 || - git_vector_init(&sc->items, 4, item_cmp) < 0 || + git_pool_init(&sc->pool, 1); + + if (git_vector_init(&sc->items, 4, item_cmp) < 0 || git_strmap_alloc(&sc->map) < 0) goto fail; diff --git a/src/transaction.c b/src/transaction.c index 92e134e5b..2c8a1e8bd 100644 --- a/src/transaction.c +++ b/src/transaction.c @@ -77,8 +77,7 @@ int git_transaction_new(git_transaction **out, git_repository *repo) assert(out && repo); - if ((error = git_pool_init(&pool, 1, 0)) < 0) - return error; + git_pool_init(&pool, 1); tx = git_pool_mallocz(&pool, sizeof(git_transaction)); if (!tx) { |