|
| 1 | +/* |
| 2 | + * Copyright (c) 2012 Thomas Harning Jr. <[email protected]> |
| 3 | + * Released under the MIT license. See included LICENSE details. |
| 4 | + */ |
| 5 | + |
| 6 | +#include "cf_bucket_manager_internal.h" |
| 7 | +#include "cf_bucket_internal.h" |
| 8 | + |
| 9 | +#include "arena/proto.h" |
| 10 | + |
| 11 | +#include <string.h> /* memset */ |
| 12 | + |
| 13 | +struct cf_bucket_manager { |
| 14 | + /* Standard allocator for buckets and associated data */ |
| 15 | + const struct arena_prototype *arena; |
| 16 | +}; |
| 17 | + |
| 18 | +int cf_bucket_manager_new(cf_bucket_manager_t *manager) |
| 19 | +{ |
| 20 | + if (!manager) { |
| 21 | + return -1; |
| 22 | + } |
| 23 | + *manager = calloc(1, sizeof(**manager)); |
| 24 | + if (!*manager) { |
| 25 | + return -1; |
| 26 | + } |
| 27 | + (*manager)->arena = ARENA_STDLIB; |
| 28 | + return 0; |
| 29 | +} |
| 30 | + |
| 31 | +int cf_bucket_manager_destroy(cf_bucket_manager_t manager) |
| 32 | +{ |
| 33 | + if (!manager) { |
| 34 | + return -1; |
| 35 | + } |
| 36 | + /* TODO: perform any state cleanup on buckets allocated */ |
| 37 | + free(manager); |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +int cf_bucket_manager_allocate(cf_bucket_t *bucket, cf_bucket_manager_t manager, size_t size) |
| 43 | +{ |
| 44 | + if (size < sizeof(**bucket)) { |
| 45 | + return -1; |
| 46 | + } |
| 47 | + *bucket = manager->arena->malloc(manager->arena, size, 0); |
| 48 | + if (!*bucket) { |
| 49 | + return -1; |
| 50 | + } |
| 51 | + memset(*bucket, 0, size); |
| 52 | + /* Store a self reference */ |
| 53 | + (*bucket)->manager = manager; |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +int cf_bucket_manager_release(cf_bucket_t bucket) |
| 59 | +{ |
| 60 | + if (!bucket) { |
| 61 | + return -1; |
| 62 | + } |
| 63 | + if (!bucket->manager) { |
| 64 | + return -2; |
| 65 | + } |
| 66 | + bucket->manager->arena->free(bucket->manager->arena, bucket); |
| 67 | + return 0; |
| 68 | +} |
0 commit comments