summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2022-01-18 18:57:06 +0000
committerAidan MacDonald <amachronic@protonmail.com>2022-02-12 10:24:32 -0500
commite8faf2f2adeb9066de3c968a57803bb262f61ee1 (patch)
tree130c2e3d904da7876ac88ea089f85f48cab5e313
parent95dfc489b5a602f68202b206c434f8485e9d3915 (diff)
downloadrockbox-e8faf2f2adeb9066de3c968a57803bb262f61ee1.tar.gz
rockbox-e8faf2f2adeb9066de3c968a57803bb262f61ee1.zip
buflib: add a common dummy callbacks struct & use it
There are various allocations that can't be moved or shrunk. Provide a global callback struct for this use case instead of making each caller declare its own dummy struct. Also fixed ROLO and x1000 installer code which incorrectly used movable allocations. Change-Id: I00088396b9826e02e69a4a33477fe1a7816374f1
-rw-r--r--apps/playlist.c10
-rw-r--r--apps/plugin.c5
-rw-r--r--apps/recorder/pcm_record.c7
-rw-r--r--apps/tagcache.c3
-rw-r--r--firmware/buflib.c6
-rw-r--r--firmware/common/zip.c4
-rw-r--r--firmware/include/buflib.h6
-rw-r--r--firmware/rolo.c2
-rw-r--r--firmware/target/mips/ingenic_x1000/installer-x1000.c2
-rw-r--r--firmware/usbstack/usb_storage.c7
-rw-r--r--lib/x1000-installer/src/xf_nandio.c2
-rw-r--r--lib/x1000-installer/src/xf_package.c2
-rw-r--r--lib/x1000-installer/test_lib/core_alloc.c7
-rw-r--r--lib/x1000-installer/test_lib/core_alloc.h10
14 files changed, 43 insertions, 30 deletions
diff --git a/apps/playlist.c b/apps/playlist.c
index e93feb6abb..a7b16d8b1b 100644
--- a/apps/playlist.c
+++ b/apps/playlist.c
@@ -2076,13 +2076,10 @@ int playlist_create(const char *dir, const char *file)
2076 2076
2077 if (file) 2077 if (file)
2078 { 2078 {
2079 /* dummy ops with no callbacks, needed because by
2080 * default buflib buffers can be moved around which must be avoided */
2081 static struct buflib_callbacks dummy_ops;
2082 int handle; 2079 int handle;
2083 size_t buflen; 2080 size_t buflen;
2084 /* use mp3 buffer for maximum load speed */ 2081 /* use mp3 buffer for maximum load speed */
2085 handle = core_alloc_maximum("temp", &buflen, &dummy_ops); 2082 handle = core_alloc_maximum("temp", &buflen, &buflib_ops_locked);
2086 if (handle > 0) 2083 if (handle > 0)
2087 { 2084 {
2088 /* load the playlist file */ 2085 /* load the playlist file */
@@ -2119,13 +2116,10 @@ int playlist_resume(void)
2119 bool sorted = true; 2116 bool sorted = true;
2120 int result = -1; 2117 int result = -1;
2121 2118
2122 /* dummy ops with no callbacks, needed because by
2123 * default buflib buffers can be moved around which must be avoided */
2124 static struct buflib_callbacks dummy_ops;
2125 /* use mp3 buffer for maximum load speed */ 2119 /* use mp3 buffer for maximum load speed */
2126 if (core_allocatable() < (1 << 10)) 2120 if (core_allocatable() < (1 << 10))
2127 talk_buffer_set_policy(TALK_BUFFER_LOOSE); /* back off voice buffer */ 2121 talk_buffer_set_policy(TALK_BUFFER_LOOSE); /* back off voice buffer */
2128 handle = core_alloc_maximum("temp", &buflen, &dummy_ops); 2122 handle = core_alloc_maximum("temp", &buflen, &buflib_ops_locked);
2129 if (handle < 0) 2123 if (handle < 0)
2130 { 2124 {
2131 splashf(HZ * 2, "%s(): OOM", __func__); 2125 splashf(HZ * 2, "%s(): OOM", __func__);
diff --git a/apps/plugin.c b/apps/plugin.c
index aa69b9ca03..8434fea07d 100644
--- a/apps/plugin.c
+++ b/apps/plugin.c
@@ -990,14 +990,11 @@ void* plugin_get_buffer(size_t *buffer_size)
990 */ 990 */
991static void* plugin_get_audio_buffer(size_t *buffer_size) 991static void* plugin_get_audio_buffer(size_t *buffer_size)
992{ 992{
993 /* dummy ops with no callbacks, needed because by
994 * default buflib buffers can be moved around which must be avoided */
995 static struct buflib_callbacks dummy_ops;
996 if (plugin_buffer_handle <= 0) 993 if (plugin_buffer_handle <= 0)
997 { 994 {
998 plugin_buffer_handle = core_alloc_maximum("plugin audio buf", 995 plugin_buffer_handle = core_alloc_maximum("plugin audio buf",
999 &plugin_buffer_size, 996 &plugin_buffer_size,
1000 &dummy_ops); 997 &buflib_ops_locked);
1001 } 998 }
1002 999
1003 if (buffer_size) 1000 if (buffer_size)
diff --git a/apps/recorder/pcm_record.c b/apps/recorder/pcm_record.c
index ef10f8a433..0f6e8469ec 100644
--- a/apps/recorder/pcm_record.c
+++ b/apps/recorder/pcm_record.c
@@ -1406,12 +1406,9 @@ static int pcmrec_handle;
1406static void on_init_recording(void) 1406static void on_init_recording(void)
1407{ 1407{
1408 send_event(RECORDING_EVENT_START, NULL); 1408 send_event(RECORDING_EVENT_START, NULL);
1409 /* dummy ops with no callbacks, needed because by 1409 /* FIXME: This buffer should play nicer and be shrinkable/movable */
1410 * default buflib buffers can be moved around which must be avoided
1411 * FIXME: This buffer should play nicer and be shrinkable/movable */
1412 static struct buflib_callbacks dummy_ops;
1413 talk_buffer_set_policy(TALK_BUFFER_LOOSE); 1410 talk_buffer_set_policy(TALK_BUFFER_LOOSE);
1414 pcmrec_handle = core_alloc_maximum("pcmrec", &rec_buffer_size, &dummy_ops); 1411 pcmrec_handle = core_alloc_maximum("pcmrec", &rec_buffer_size, &buflib_ops_locked);
1415 if (pcmrec_handle <= 0) 1412 if (pcmrec_handle <= 0)
1416 /* someone is abusing core_alloc_maximum(). Fix this evil guy instead of 1413 /* someone is abusing core_alloc_maximum(). Fix this evil guy instead of
1417 * trying to handle OOM without hope */ 1414 * trying to handle OOM without hope */
diff --git a/apps/tagcache.c b/apps/tagcache.c
index 37f443e036..fc06005c1d 100644
--- a/apps/tagcache.c
+++ b/apps/tagcache.c
@@ -328,8 +328,7 @@ static void allocate_tempbuf(void)
328#else /* !__PCTOOL__ */ 328#else /* !__PCTOOL__ */
329 /* Need to pass dummy ops to prevent the buffer being moved 329 /* Need to pass dummy ops to prevent the buffer being moved
330 * out from under us, since we yield during the tagcache commit. */ 330 * out from under us, since we yield during the tagcache commit. */
331 static struct buflib_callbacks dummy_ops; 331 tempbuf_handle = core_alloc_maximum("tc tempbuf", &size, &buflib_ops_locked);
332 tempbuf_handle = core_alloc_maximum("tc tempbuf", &size, &dummy_ops);
333 if (tempbuf_handle > 0) 332 if (tempbuf_handle > 0)
334 { 333 {
335 tempbuf = core_get_data(tempbuf_handle); 334 tempbuf = core_get_data(tempbuf_handle);
diff --git a/firmware/buflib.c b/firmware/buflib.c
index 0e90e7fe72..c6ec011653 100644
--- a/firmware/buflib.c
+++ b/firmware/buflib.c
@@ -97,6 +97,12 @@
97 97
98#define BPANICF panicf 98#define BPANICF panicf
99 99
100struct buflib_callbacks buflib_ops_locked = {
101 .move_callback = NULL,
102 .shrink_callback = NULL,
103 .sync_callback = NULL,
104};
105
100#define IS_MOVABLE(a) (!a[2].ops || a[2].ops->move_callback) 106#define IS_MOVABLE(a) (!a[2].ops || a[2].ops->move_callback)
101static union buflib_data* find_first_free(struct buflib_context *ctx); 107static union buflib_data* find_first_free(struct buflib_context *ctx);
102static union buflib_data* find_block_before(struct buflib_context *ctx, 108static union buflib_data* find_block_before(struct buflib_context *ctx,
diff --git a/firmware/common/zip.c b/firmware/common/zip.c
index 9512d6c239..36b90a9223 100644
--- a/firmware/common/zip.c
+++ b/firmware/common/zip.c
@@ -32,9 +32,7 @@
32#include "crc32.h" 32#include "crc32.h"
33#include "rbendian.h" 33#include "rbendian.h"
34 34
35#define zip_core_alloc(N) core_alloc_ex("zip",(N),&dummy_ops) 35#define zip_core_alloc(N) core_alloc_ex("zip",(N),&buflib_ops_locked)
36
37static struct buflib_callbacks dummy_ops;
38 36
39enum { 37enum {
40 ZIP_SIG_ED = 0x06054b50, 38 ZIP_SIG_ED = 0x06054b50,
diff --git a/firmware/include/buflib.h b/firmware/include/buflib.h
index 7f534c6ce0..e805ebbf1b 100644
--- a/firmware/include/buflib.h
+++ b/firmware/include/buflib.h
@@ -129,6 +129,12 @@ struct buflib_callbacks {
129 void (*sync_callback)(int handle, bool sync_on); 129 void (*sync_callback)(int handle, bool sync_on);
130}; 130};
131 131
132/** A set of all NULL callbacks for use with allocations that need to stay
133 * locked in RAM and not moved or shrunk. These type of allocations should
134 * be avoided as much as possible to avoid memory fragmentation but it can
135 * suitable for short-lived allocations. */
136extern struct buflib_callbacks buflib_ops_locked;
137
132#define BUFLIB_SHRINK_SIZE_MASK (~BUFLIB_SHRINK_POS_MASK) 138#define BUFLIB_SHRINK_SIZE_MASK (~BUFLIB_SHRINK_POS_MASK)
133#define BUFLIB_SHRINK_POS_FRONT (1u<<31) 139#define BUFLIB_SHRINK_POS_FRONT (1u<<31)
134#define BUFLIB_SHRINK_POS_BACK (1u<<30) 140#define BUFLIB_SHRINK_POS_BACK (1u<<30)
diff --git a/firmware/rolo.c b/firmware/rolo.c
index 5f936c95f4..f95fc4bd4d 100644
--- a/firmware/rolo.c
+++ b/firmware/rolo.c
@@ -242,7 +242,7 @@ int rolo_load(const char* filename)
242 242
243 /* get the system buffer. release only in case of error, otherwise 243 /* get the system buffer. release only in case of error, otherwise
244 * we don't return anyway */ 244 * we don't return anyway */
245 rolo_handle = core_alloc_maximum("rolo", &filebuf_size, NULL); 245 rolo_handle = core_alloc_maximum("rolo", &filebuf_size, &buflib_ops_locked);
246 if (rolo_handle < 0) 246 if (rolo_handle < 0)
247 { 247 {
248 rolo_error("OOM"); 248 rolo_error("OOM");
diff --git a/firmware/target/mips/ingenic_x1000/installer-x1000.c b/firmware/target/mips/ingenic_x1000/installer-x1000.c
index 0a09ad0e91..66aa42d4a1 100644
--- a/firmware/target/mips/ingenic_x1000/installer-x1000.c
+++ b/firmware/target/mips/ingenic_x1000/installer-x1000.c
@@ -148,7 +148,7 @@ static int updater_init(struct updater* u)
148 148
149 /* buf_len is a bit oversized here, but it's not really important */ 149 /* buf_len is a bit oversized here, but it's not really important */
150 u->buf_len = u->img_len + sizeof(mtar_t) + 2*CACHEALIGN_SIZE; 150 u->buf_len = u->img_len + sizeof(mtar_t) + 2*CACHEALIGN_SIZE;
151 u->buf_hnd = core_alloc("boot_image", u->buf_len); 151 u->buf_hnd = core_alloc_ex("boot_image", u->buf_len, &buflib_ops_locked);
152 if(u->buf_hnd < 0) { 152 if(u->buf_hnd < 0) {
153 rc = IERR_OUT_OF_MEMORY; 153 rc = IERR_OUT_OF_MEMORY;
154 goto error; 154 goto error;
diff --git a/firmware/usbstack/usb_storage.c b/firmware/usbstack/usb_storage.c
index a32cf185e7..eb82f72eae 100644
--- a/firmware/usbstack/usb_storage.c
+++ b/firmware/usbstack/usb_storage.c
@@ -449,12 +449,11 @@ void usb_storage_init_connection(void)
449#endif 449#endif
450#else 450#else
451 unsigned char * buffer; 451 unsigned char * buffer;
452 /* dummy ops with no callbacks, needed because by
453 * default buflib buffers can be moved around which must be avoided */
454 static struct buflib_callbacks dummy_ops;
455 452
456 // Add 31 to handle worst-case misalignment 453 // Add 31 to handle worst-case misalignment
457 usb_handle = core_alloc_ex("usb storage", ALLOCATE_BUFFER_SIZE + MAX_CBW_SIZE + 31, &dummy_ops); 454 usb_handle = core_alloc_ex("usb storage",
455 ALLOCATE_BUFFER_SIZE + MAX_CBW_SIZE + 31,
456 &buflib_ops_locked);
458 if (usb_handle < 0) 457 if (usb_handle < 0)
459 panicf("%s(): OOM", __func__); 458 panicf("%s(): OOM", __func__);
460 459
diff --git a/lib/x1000-installer/src/xf_nandio.c b/lib/x1000-installer/src/xf_nandio.c
index ba79cbbcbf..29ff9d9120 100644
--- a/lib/x1000-installer/src/xf_nandio.c
+++ b/lib/x1000-installer/src/xf_nandio.c
@@ -51,7 +51,7 @@ int xf_nandio_init(struct xf_nandio* nio)
51 alloc_size += CACHEALIGN_SIZE - 1; 51 alloc_size += CACHEALIGN_SIZE - 1;
52 alloc_size += nio->block_size * 2; 52 alloc_size += nio->block_size * 2;
53 53
54 nio->alloc_handle = core_alloc("xf_nandio", alloc_size); 54 nio->alloc_handle = core_alloc_ex("xf_nandio", alloc_size, &buflib_ops_locked);
55 if(nio->alloc_handle < 0) { 55 if(nio->alloc_handle < 0) {
56 rc = XF_E_OUT_OF_MEMORY; 56 rc = XF_E_OUT_OF_MEMORY;
57 goto out_nclose; 57 goto out_nclose;
diff --git a/lib/x1000-installer/src/xf_package.c b/lib/x1000-installer/src/xf_package.c
index 78bddded68..04b32cdcb0 100644
--- a/lib/x1000-installer/src/xf_package.c
+++ b/lib/x1000-installer/src/xf_package.c
@@ -49,7 +49,7 @@ static int pkg_alloc(struct xf_package* pkg)
49 alloc_size += ALIGN_UP_P2(METADATA_SIZE, 3); 49 alloc_size += ALIGN_UP_P2(METADATA_SIZE, 3);
50 alloc_size += 7; /* for alignment */ 50 alloc_size += 7; /* for alignment */
51 51
52 pkg->alloc_handle = core_alloc("xf_package", alloc_size); 52 pkg->alloc_handle = core_alloc_ex("xf_package", alloc_size, &buflib_ops_locked);
53 if(pkg->alloc_handle < 0) 53 if(pkg->alloc_handle < 0)
54 return XF_E_OUT_OF_MEMORY; 54 return XF_E_OUT_OF_MEMORY;
55 55
diff --git a/lib/x1000-installer/test_lib/core_alloc.c b/lib/x1000-installer/test_lib/core_alloc.c
index 5d4edb03f7..719670f8f2 100644
--- a/lib/x1000-installer/test_lib/core_alloc.c
+++ b/lib/x1000-installer/test_lib/core_alloc.c
@@ -25,6 +25,7 @@
25#define N_POINTERS 100 25#define N_POINTERS 100
26 26
27static void* pointers[N_POINTERS]; 27static void* pointers[N_POINTERS];
28struct buflib_callbacks buflib_ops_locked = {NULL, NULL, NULL};
28 29
29int core_alloc(const char* name, size_t size) 30int core_alloc(const char* name, size_t size)
30{ 31{
@@ -46,6 +47,12 @@ int core_alloc(const char* name, size_t size)
46 return -1; 47 return -1;
47} 48}
48 49
50int core_alloc_ex(const char* name, size_t size, struct buflib_callbacks* cb)
51{
52 (void)cb;
53 return core_alloc(name, size);
54}
55
49int core_free(int handle) 56int core_free(int handle)
50{ 57{
51 if(handle > 0) { 58 if(handle > 0) {
diff --git a/lib/x1000-installer/test_lib/core_alloc.h b/lib/x1000-installer/test_lib/core_alloc.h
index 6fb06649fb..2c77e3c274 100644
--- a/lib/x1000-installer/test_lib/core_alloc.h
+++ b/lib/x1000-installer/test_lib/core_alloc.h
@@ -25,8 +25,18 @@
25#define CORE_ALLOC_H 25#define CORE_ALLOC_H
26 26
27#include <stddef.h> 27#include <stddef.h>
28#include <stdbool.h>
29
30struct buflib_callbacks {
31 int (*move_callback)(int handle, void* current, void* new);
32 int (*shrink_callback)(int handle, unsigned hints, void* start, size_t old_size);
33 void (*sync_callback)(int handle, bool sync_on);
34};
35
36extern struct buflib_callbacks buflib_ops_locked;
28 37
29int core_alloc(const char* name, size_t size); 38int core_alloc(const char* name, size_t size);
39int core_alloc_ex(const char* name, size_t size, struct buflib_callbacks* cb);
30int core_free(int handle); 40int core_free(int handle);
31void* core_get_data(int handle); 41void* core_get_data(int handle);
32 42