diff options
-rw-r--r-- | firmware/buflib.c | 50 |
1 files changed, 15 insertions, 35 deletions
diff --git a/firmware/buflib.c b/firmware/buflib.c index 2ce9cc344c..ea57dbb379 100644 --- a/firmware/buflib.c +++ b/firmware/buflib.c | |||
@@ -118,7 +118,6 @@ enum { | |||
118 | fidx_LEN, /* length of the block, must come first */ | 118 | fidx_LEN, /* length of the block, must come first */ |
119 | fidx_HANDLE, /* pointer to entry in the handle table */ | 119 | fidx_HANDLE, /* pointer to entry in the handle table */ |
120 | fidx_OPS, /* pointer to an ops struct */ | 120 | fidx_OPS, /* pointer to an ops struct */ |
121 | fidx_NAME, /* name, optional and variable length, must come last */ | ||
122 | }; | 121 | }; |
123 | 122 | ||
124 | /* Backward indices, used to index a block end pointer as block[-bidx_XXX] */ | 123 | /* Backward indices, used to index a block end pointer as block[-bidx_XXX] */ |
@@ -131,8 +130,7 @@ enum { | |||
131 | bidx_BSIZE, /* total size of the block header */ | 130 | bidx_BSIZE, /* total size of the block header */ |
132 | }; | 131 | }; |
133 | 132 | ||
134 | /* Number of fields in the block header, excluding the name, which is | 133 | /* Number of fields in the block header. Note that bidx_USER is not an |
135 | * accounted for using the BSIZE field. Note that bidx_USER is not an | ||
136 | * actual field so it is not included in the count. */ | 134 | * actual field so it is not included in the count. */ |
137 | #ifdef BUFLIB_HAS_CRC | 135 | #ifdef BUFLIB_HAS_CRC |
138 | # define BUFLIB_NUM_FIELDS 6 | 136 | # define BUFLIB_NUM_FIELDS 6 |
@@ -192,11 +190,6 @@ static void check_block_crc(struct buflib_context *ctx, | |||
192 | union buflib_data *block, | 190 | union buflib_data *block, |
193 | union buflib_data *block_end); | 191 | union buflib_data *block_end); |
194 | 192 | ||
195 | static inline char* get_block_name(union buflib_data *block) | ||
196 | { | ||
197 | return (char*)&block[fidx_NAME]; | ||
198 | } | ||
199 | |||
200 | /* Initialize buffer manager */ | 193 | /* Initialize buffer manager */ |
201 | void | 194 | void |
202 | buflib_init(struct buflib_context *ctx, void *buf, size_t size) | 195 | buflib_init(struct buflib_context *ctx, void *buf, size_t size) |
@@ -401,8 +394,8 @@ move_block(struct buflib_context* ctx, union buflib_data* block, int shift) | |||
401 | return false; | 394 | return false; |
402 | 395 | ||
403 | int handle = ctx->handle_table - h_entry; | 396 | int handle = ctx->handle_table - h_entry; |
404 | BDEBUGF("%s(): moving \"%s\"(id=%d) by %d(%d)\n", __func__, | 397 | BDEBUGF("%s(): moving id=%d by %d(%d)\n", __func__, |
405 | get_block_name(block), handle, shift, shift*(int)sizeof(union buflib_data)); | 398 | handle, shift, shift*(int)sizeof(union buflib_data)); |
406 | new_block = block + shift; | 399 | new_block = block + shift; |
407 | new_start = h_entry->alloc + shift*sizeof(union buflib_data); | 400 | new_start = h_entry->alloc + shift*sizeof(union buflib_data); |
408 | 401 | ||
@@ -645,8 +638,7 @@ buflib_alloc(struct buflib_context *ctx, size_t size) | |||
645 | 638 | ||
646 | /* Allocate a buffer of size bytes, returning a handle for it. | 639 | /* Allocate a buffer of size bytes, returning a handle for it. |
647 | * | 640 | * |
648 | * The additional name parameter gives the allocation a human-readable name, | 641 | * The ops parameter points to caller-implemented callbacks for moving and |
649 | * the ops parameter points to caller-implemented callbacks for moving and | ||
650 | * shrinking. | 642 | * shrinking. |
651 | * | 643 | * |
652 | * If you pass NULL for "ops", buffers are movable by default. | 644 | * If you pass NULL for "ops", buffers are movable by default. |
@@ -658,12 +650,12 @@ int | |||
658 | buflib_alloc_ex(struct buflib_context *ctx, size_t size, const char *name, | 650 | buflib_alloc_ex(struct buflib_context *ctx, size_t size, const char *name, |
659 | struct buflib_callbacks *ops) | 651 | struct buflib_callbacks *ops) |
660 | { | 652 | { |
653 | (void)name; | ||
654 | |||
661 | union buflib_data *handle, *block; | 655 | union buflib_data *handle, *block; |
662 | size_t name_len = name ? B_ALIGN_UP(strlen(name)+1) : 0; | ||
663 | bool last; | 656 | bool last; |
664 | /* This really is assigned a value before use */ | 657 | /* This really is assigned a value before use */ |
665 | int block_len; | 658 | int block_len; |
666 | size += name_len; | ||
667 | size = (size + sizeof(union buflib_data) - 1) / | 659 | size = (size + sizeof(union buflib_data) - 1) / |
668 | sizeof(union buflib_data) | 660 | sizeof(union buflib_data) |
669 | + BUFLIB_NUM_FIELDS; | 661 | + BUFLIB_NUM_FIELDS; |
@@ -749,10 +741,8 @@ buffer_alloc: | |||
749 | block[fidx_LEN].val = size; | 741 | block[fidx_LEN].val = size; |
750 | block[fidx_HANDLE].handle = handle; | 742 | block[fidx_HANDLE].handle = handle; |
751 | block[fidx_OPS].ops = ops; | 743 | block[fidx_OPS].ops = ops; |
752 | if (name_len > 0) | ||
753 | strcpy(get_block_name(block), name); | ||
754 | 744 | ||
755 | size_t bsize = BUFLIB_NUM_FIELDS + name_len/sizeof(union buflib_data); | 745 | size_t bsize = BUFLIB_NUM_FIELDS; |
756 | union buflib_data *block_end = block + bsize; | 746 | union buflib_data *block_end = block + bsize; |
757 | block_end[-bidx_PIN].pincount = 0; | 747 | block_end[-bidx_PIN].pincount = 0; |
758 | block_end[-bidx_BSIZE].val = bsize; | 748 | block_end[-bidx_BSIZE].val = bsize; |
@@ -760,8 +750,8 @@ buffer_alloc: | |||
760 | 750 | ||
761 | handle->alloc = (char*)&block_end[-bidx_USER]; | 751 | handle->alloc = (char*)&block_end[-bidx_USER]; |
762 | 752 | ||
763 | BDEBUGF("buflib_alloc_ex: size=%d handle=%p clb=%p name=\"%s\"\n", | 753 | BDEBUGF("buflib_alloc_ex: size=%d handle=%p clb=%p\n", |
764 | (unsigned int)size, (void *)handle, (void *)ops, name ? name : ""); | 754 | (unsigned int)size, (void *)handle, (void *)ops); |
765 | 755 | ||
766 | block += size; | 756 | block += size; |
767 | /* alloc_end must be kept current if we're taking the last block. */ | 757 | /* alloc_end must be kept current if we're taking the last block. */ |
@@ -868,7 +858,6 @@ free_space_at_end(struct buflib_context* ctx) | |||
868 | ptrdiff_t diff = (ctx->last_handle - ctx->alloc_end - BUFLIB_NUM_FIELDS); | 858 | ptrdiff_t diff = (ctx->last_handle - ctx->alloc_end - BUFLIB_NUM_FIELDS); |
869 | diff -= 16; /* space for future handles */ | 859 | diff -= 16; /* space for future handles */ |
870 | diff *= sizeof(union buflib_data); /* make it bytes */ | 860 | diff *= sizeof(union buflib_data); /* make it bytes */ |
871 | diff -= 16; /* reserve 16 for the name */ | ||
872 | 861 | ||
873 | if (diff > 0) | 862 | if (diff > 0) |
874 | return diff; | 863 | return diff; |
@@ -956,9 +945,6 @@ buflib_available(struct buflib_context* ctx) | |||
956 | int | 945 | int |
957 | buflib_alloc_maximum(struct buflib_context* ctx, const char* name, size_t *size, struct buflib_callbacks *ops) | 946 | buflib_alloc_maximum(struct buflib_context* ctx, const char* name, size_t *size, struct buflib_callbacks *ops) |
958 | { | 947 | { |
959 | /* limit name to 16 since that's what buflib_available() accounts for it */ | ||
960 | char buf[16]; | ||
961 | |||
962 | /* ignore ctx->compact because it's true if all movable blocks are contiguous | 948 | /* ignore ctx->compact because it's true if all movable blocks are contiguous |
963 | * even if the buffer has holes due to unmovable allocations */ | 949 | * even if the buffer has holes due to unmovable allocations */ |
964 | unsigned hints; | 950 | unsigned hints; |
@@ -974,9 +960,7 @@ buflib_alloc_maximum(struct buflib_context* ctx, const char* name, size_t *size, | |||
974 | if (*size <= 0) /* OOM */ | 960 | if (*size <= 0) /* OOM */ |
975 | return -1; | 961 | return -1; |
976 | 962 | ||
977 | strmemccpy(buf, name, sizeof(buf)); | 963 | return buflib_alloc_ex(ctx, *size, name, ops); |
978 | |||
979 | return buflib_alloc_ex(ctx, *size, buf, ops); | ||
980 | } | 964 | } |
981 | 965 | ||
982 | /* Shrink the allocation indicated by the handle according to new_start and | 966 | /* Shrink the allocation indicated by the handle according to new_start and |
@@ -1091,13 +1075,9 @@ unsigned buflib_pin_count(struct buflib_context *ctx, int handle) | |||
1091 | 1075 | ||
1092 | const char* buflib_get_name(struct buflib_context *ctx, int handle) | 1076 | const char* buflib_get_name(struct buflib_context *ctx, int handle) |
1093 | { | 1077 | { |
1094 | union buflib_data *data = handle_to_block_end(ctx, handle); | 1078 | (void)ctx; |
1095 | size_t len = data[-bidx_BSIZE].val; | 1079 | (void)handle; |
1096 | if (len <= BUFLIB_NUM_FIELDS) | 1080 | return ""; |
1097 | return NULL; | ||
1098 | |||
1099 | data -= len; | ||
1100 | return get_block_name(data); | ||
1101 | } | 1081 | } |
1102 | 1082 | ||
1103 | #ifdef DEBUG | 1083 | #ifdef DEBUG |
@@ -1153,9 +1133,9 @@ void buflib_print_block_at(struct buflib_context *ctx, int block_num, | |||
1153 | 1133 | ||
1154 | if (block_num-- == 0) | 1134 | if (block_num-- == 0) |
1155 | { | 1135 | { |
1156 | snprintf(buf, bufsize, "%8p: val: %4ld (%s)", | 1136 | snprintf(buf, bufsize, "%8p: val: %4ld (%sallocated)", |
1157 | block, (long)block->val, | 1137 | block, (long)block->val, |
1158 | block->val > 0 ? get_block_name(block) : "<unallocated>"); | 1138 | block->val > 0 ? "" : "un"); |
1159 | } | 1139 | } |
1160 | } | 1140 | } |
1161 | } | 1141 | } |