summaryrefslogtreecommitdiff
path: root/apps/plugins/xrick/system/sysmem_rockbox.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/xrick/system/sysmem_rockbox.c')
-rw-r--r--apps/plugins/xrick/system/sysmem_rockbox.c156
1 files changed, 156 insertions, 0 deletions
diff --git a/apps/plugins/xrick/system/sysmem_rockbox.c b/apps/plugins/xrick/system/sysmem_rockbox.c
new file mode 100644
index 0000000000..06a683a463
--- /dev/null
+++ b/apps/plugins/xrick/system/sysmem_rockbox.c
@@ -0,0 +1,156 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include "xrick/system/system.h"
26
27#include "xrick/debug.h"
28
29#include "plugin.h"
30
31/*
32 * Local variables
33 */
34enum
35{
36 ALIGNMENT = sizeof(void*) /* this is more of an educated guess; might want to adjust for your specific architecture */
37};
38static U8 * stackBuffer;
39static U8 * stackTop;
40static size_t stackSize;
41static size_t stackMaxSize;
42static bool isMemoryInitialised = false;
43IFDEBUG_MEMORY( static size_t maxUsedMemory = 0; );
44
45/*
46 * Initialise memory stack
47 */
48bool sysmem_init(void)
49{
50 if (isMemoryInitialised)
51 {
52 return true;
53 }
54
55 if (rb->audio_status())
56 {
57 /* Playback must be stopped the entire time the sound buffer is used.*/
58 rb->audio_stop();
59 }
60
61 stackBuffer = rb->plugin_get_audio_buffer(&stackMaxSize);
62 stackTop = stackBuffer;
63 stackSize = 0;
64 isMemoryInitialised = true;
65 return true;
66}
67
68/*
69 * Cleanup memory stack
70 */
71void sysmem_shutdown(void)
72{
73 if (!isMemoryInitialised)
74 {
75 return;
76 }
77
78 if (stackTop != stackBuffer || stackSize != 0)
79 {
80 sys_error("(memory) improper deallocation detected");
81 }
82
83 IFDEBUG_MEMORY(
84 sys_printf("xrick/memory: max memory usage was %u bytes\n", maxUsedMemory);
85 );
86
87 isMemoryInitialised = false;
88}
89
90/*
91 * Allocate a memory-aligned block on top of the memory stack
92 */
93void *sysmem_push(size_t size)
94{
95 uintptr_t alignedPtr;
96 size_t * allocatedSizePtr;
97
98 size_t neededSize = sizeof(size_t) + size + (ALIGNMENT - 1);
99 if (stackSize + neededSize > stackMaxSize)
100 {
101 sys_error("(memory) tried to allocate a block when memory full");
102 return NULL;
103 }
104
105 alignedPtr = (((uintptr_t)stackTop) + sizeof(size_t) + ALIGNMENT) & ~((uintptr_t)(ALIGNMENT - 1));
106
107 allocatedSizePtr = (size_t *)(alignedPtr);
108 allocatedSizePtr[-1] = neededSize;
109
110 stackTop += neededSize;
111 stackSize += neededSize;
112
113 IFDEBUG_MEMORY(
114 sys_printf("xrick/memory: allocated %u bytes\n", neededSize);
115 if (stackSize > maxUsedMemory) maxUsedMemory = stackSize;
116 );
117
118 return (void *)alignedPtr;
119}
120
121/*
122 * Release block from the top of the memory stack
123 */
124void sysmem_pop(void * alignedPtr)
125{
126 size_t allocatedSize;
127
128 if (!alignedPtr)
129 {
130 return;
131 }
132
133 if (stackSize == 0)
134 {
135 sys_error("(memory) tried to release a block when memory empty");
136 return;
137 }
138
139 allocatedSize = ((size_t *)(alignedPtr))[-1];
140 stackTop -= allocatedSize;
141 stackSize -= allocatedSize;
142
143 IFDEBUG_MEMORY(
144 if ((uintptr_t)alignedPtr != ((((uintptr_t)stackTop) + sizeof(size_t) + ALIGNMENT) & ~((uintptr_t)(ALIGNMENT - 1))))
145 {
146 sys_error("(memory) tried to release a wrong block");
147 return;
148 }
149 );
150
151 IFDEBUG_MEMORY(
152 sys_printf("xrick/memory: released %u bytes\n", allocatedSize);
153 );
154}
155
156/* eof */