summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2014-02-02 14:43:45 +0100
committerThomas Martitz <kugel@rockbox.org>2014-02-02 19:40:38 +0100
commit4ce1deacfd4e5440cc82237ebc5fafbaeea64763 (patch)
tree7ff4664819704fd890d3e5ea95349c8500f9c4c7
parentd66346789ccdf685a6720a739b88f194f56a60e2 (diff)
downloadrockbox-4ce1deacfd4e5440cc82237ebc5fafbaeea64763.tar.gz
rockbox-4ce1deacfd4e5440cc82237ebc5fafbaeea64763.zip
buflib: Properly support allocations without any name, to avoid wasting space
in micro-allocation scenarios. Change-Id: I97a065bcfba8e0fda9b1670445e839e267c769c8
-rw-r--r--firmware/buflib.c28
-rw-r--r--firmware/core_alloc.c6
-rw-r--r--firmware/include/buflib.h8
-rw-r--r--firmware/include/core_alloc.h7
4 files changed, 27 insertions, 22 deletions
diff --git a/firmware/buflib.c b/firmware/buflib.c
index 294d2926d3..27a6965ee0 100644
--- a/firmware/buflib.c
+++ b/firmware/buflib.c
@@ -105,6 +105,8 @@ void
105buflib_init(struct buflib_context *ctx, void *buf, size_t size) 105buflib_init(struct buflib_context *ctx, void *buf, size_t size)
106{ 106{
107 union buflib_data *bd_buf = buf; 107 union buflib_data *bd_buf = buf;
108 BDEBUGF("buflib initialized with %lu.%02lu kiB",
109 (unsigned long)size / 1024, ((unsigned long)size%1000)/10);
108 110
109 /* Align on sizeof(buflib_data), to prevent unaligned access */ 111 /* Align on sizeof(buflib_data), to prevent unaligned access */
110 ALIGN_BUFFER(bd_buf, size, sizeof(union buflib_data)); 112 ALIGN_BUFFER(bd_buf, size, sizeof(union buflib_data));
@@ -119,9 +121,6 @@ buflib_init(struct buflib_context *ctx, void *buf, size_t size)
119 */ 121 */
120 ctx->alloc_end = bd_buf; 122 ctx->alloc_end = bd_buf;
121 ctx->compact = true; 123 ctx->compact = true;
122
123 BDEBUGF("buflib initialized with %lu.%2lu kiB",
124 (unsigned long)size / 1024, ((unsigned long)size%1000)/10);
125} 124}
126 125
127bool buflib_context_relocate(struct buflib_context *ctx, void *buf) 126bool buflib_context_relocate(struct buflib_context *ctx, void *buf)
@@ -206,12 +205,16 @@ void handle_free(struct buflib_context *ctx, union buflib_data *handle)
206} 205}
207 206
208/* Get the start block of an allocation */ 207/* Get the start block of an allocation */
209static union buflib_data* handle_to_block(struct buflib_context* ctx, int handle) 208static inline
209union buflib_data* handle_to_block(struct buflib_context* ctx, int handle)
210{ 210{
211 union buflib_data* name_field = 211 union buflib_data *data = ALIGN_DOWN(buflib_get_data(ctx, handle), sizeof (*data));
212 (union buflib_data*)buflib_get_name(ctx, handle); 212 /* this is a valid case, e.g. during buflib_alloc_ex() when the handle
213 213 * has already been allocated but not the data */
214 return name_field ? name_field - 3 : NULL; 214 if (!data)
215 return NULL;
216 volatile size_t len = data[-2].val;
217 return data - (len + 4);
215} 218}
216 219
217/* Shrink the handle table, returning true if its size was reduced, false if 220/* Shrink the handle table, returning true if its size was reduced, false if
@@ -480,7 +483,7 @@ buflib_buffer_in(struct buflib_context *ctx, int size)
480int 483int
481buflib_alloc(struct buflib_context *ctx, size_t size) 484buflib_alloc(struct buflib_context *ctx, size_t size)
482{ 485{
483 return buflib_alloc_ex(ctx, size, "<anonymous>", NULL); 486 return buflib_alloc_ex(ctx, size, NULL, NULL);
484} 487}
485 488
486/* Allocate a buffer of size bytes, returning a handle for it. 489/* Allocate a buffer of size bytes, returning a handle for it.
@@ -588,7 +591,8 @@ buffer_alloc:
588 block->val = size; 591 block->val = size;
589 block[1].handle = handle; 592 block[1].handle = handle;
590 block[2].ops = ops; 593 block[2].ops = ops;
591 strcpy(block[3].name, name); 594 if (name_len > 0)
595 strcpy(block[3].name, name);
592 name_len_slot = (union buflib_data*)B_ALIGN_UP(block[3].name + name_len); 596 name_len_slot = (union buflib_data*)B_ALIGN_UP(block[3].name + name_len);
593 name_len_slot->val = 1 + name_len/sizeof(union buflib_data); 597 name_len_slot->val = 1 + name_len/sizeof(union buflib_data);
594 crc_slot = (union buflib_data*)(name_len_slot + 1); 598 crc_slot = (union buflib_data*)(name_len_slot + 1);
@@ -599,7 +603,7 @@ buffer_alloc:
599 603
600 BDEBUGF("buflib_alloc_ex: size=%d handle=%p clb=%p crc=0x%0x name=\"%s\"\n", 604 BDEBUGF("buflib_alloc_ex: size=%d handle=%p clb=%p crc=0x%0x name=\"%s\"\n",
601 (unsigned int)size, (void *)handle, (void *)ops, 605 (unsigned int)size, (void *)handle, (void *)ops,
602 (unsigned int)crc_slot->crc, block[3].name); 606 (unsigned int)crc_slot->crc, name ? block[3].name:"");
603 607
604 block += size; 608 block += size;
605 /* alloc_end must be kept current if we're taking the last block. */ 609 /* alloc_end must be kept current if we're taking the last block. */
@@ -889,8 +893,6 @@ buflib_shrink(struct buflib_context* ctx, int handle, void* new_start, size_t ne
889const char* buflib_get_name(struct buflib_context *ctx, int handle) 893const char* buflib_get_name(struct buflib_context *ctx, int handle)
890{ 894{
891 union buflib_data *data = ALIGN_DOWN(buflib_get_data(ctx, handle), sizeof (*data)); 895 union buflib_data *data = ALIGN_DOWN(buflib_get_data(ctx, handle), sizeof (*data));
892 if (!data)
893 return NULL;
894 size_t len = data[-2].val; 896 size_t len = data[-2].val;
895 if (len <= 1) 897 if (len <= 1)
896 return NULL; 898 return NULL;
diff --git a/firmware/core_alloc.c b/firmware/core_alloc.c
index 68c400f669..58e12141e1 100644
--- a/firmware/core_alloc.c
+++ b/firmware/core_alloc.c
@@ -87,6 +87,12 @@ bool core_shrink(int handle, void* new_start, size_t new_size)
87 return buflib_shrink(&core_ctx, handle, new_start, new_size); 87 return buflib_shrink(&core_ctx, handle, new_start, new_size);
88} 88}
89 89
90const char* core_get_name(int handle)
91{
92 const char *name = buflib_get_name(&core_ctx, handle);
93 return name ?: "<anonymous>";
94}
95
90int core_get_num_blocks(void) 96int core_get_num_blocks(void)
91{ 97{
92 return buflib_get_num_blocks(&core_ctx); 98 return buflib_get_num_blocks(&core_ctx);
diff --git a/firmware/include/buflib.h b/firmware/include/buflib.h
index 30484431f8..e912429b1f 100644
--- a/firmware/include/buflib.h
+++ b/firmware/include/buflib.h
@@ -303,12 +303,14 @@ void buflib_buffer_in(struct buflib_context *ctx, int size);
303/* debugging */ 303/* debugging */
304 304
305/** 305/**
306 * Returns the name, as given to core_alloc() and core_allloc_ex(), of the 306 * Returns the name, as given to buflib_alloc() and buflib_allloc_ex(), of the
307 * allocation associated with the given handle 307 * allocation associated with the given handle. As naming allocations
308 * is optional, there might be no name associated.
308 * 309 *
309 * handle: The handle indicating the allocation 310 * handle: The handle indicating the allocation
310 * 311 *
311 * Returns: A pointer to the string identifier of the allocation 312 * Returns: A pointer to the string identifier of the allocation, or NULL
313 * if none was specified with buflib_alloc_ex/(.
312 */ 314 */
313const char* buflib_get_name(struct buflib_context *ctx, int handle); 315const char* buflib_get_name(struct buflib_context *ctx, int handle);
314 316
diff --git a/firmware/include/core_alloc.h b/firmware/include/core_alloc.h
index 095cb5da11..67fe99dfdc 100644
--- a/firmware/include/core_alloc.h
+++ b/firmware/include/core_alloc.h
@@ -17,6 +17,7 @@ bool core_shrink(int handle, void* new_start, size_t new_size);
17int core_free(int handle); 17int core_free(int handle);
18size_t core_available(void); 18size_t core_available(void);
19size_t core_allocatable(void); 19size_t core_allocatable(void);
20const char* core_get_name(int handle);
20#ifdef DEBUG 21#ifdef DEBUG
21void core_check_valid(void); 22void core_check_valid(void);
22#endif 23#endif
@@ -43,10 +44,4 @@ static inline void* core_get_data(int handle)
43 return buflib_get_data(&core_ctx, handle); 44 return buflib_get_data(&core_ctx, handle);
44} 45}
45 46
46static inline const char* core_get_name(int handle)
47{
48 extern struct buflib_context core_ctx;
49 return buflib_get_name(&core_ctx, handle);
50}
51
52#endif /* __CORE_ALLOC_H__ */ 47#endif /* __CORE_ALLOC_H__ */