summaryrefslogtreecommitdiff
path: root/firmware/test/memory/memory-block.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/test/memory/memory-block.c')
-rw-r--r--firmware/test/memory/memory-block.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/firmware/test/memory/memory-block.c b/firmware/test/memory/memory-block.c
new file mode 100644
index 0000000000..2c29d255e2
--- /dev/null
+++ b/firmware/test/memory/memory-block.c
@@ -0,0 +1,77 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id:
9 *
10 * Copyright (C) 2002 by Alan Korr
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#ifndef __LIBRARY_MEMORY_C__
20# error "This header file must be included ONLY from memory.c."
21#endif
22#ifndef __LIBRARY_MEMORY_BLOCK_H__
23# define __LIBRARY_MEMORY_BLOCK_H__
24
25static struct memory_cache *free_block_cache[MEMORY_PAGE_MINIMAL_ORDER - 2];
26
27///////////////////////////////////////////////////////////////////////////////
28// MEMORY BLOCK :
29/////////////////
30//
31// - memory_allocate_block : allocate a power-of-2-sized block (or a page)
32// - memory_release_block : release a power-of-2-sized block (or a page)
33//
34
35static inline void *allocate_small_block (int order)
36 {
37 struct memory_cache *cache = free_block_cache[order - 2];
38 do
39 {
40 if (cache)
41 return memory_cache_allocate (cache);
42 }
43 while ((free_block_cache[order] = cache = memory_create_cache (size,0,0)));
44 return MEMORY_RETURN_FAILURE;
45 }
46
47void *memory_allocate_block (int order)
48 {
49 if (order < 2)
50 order = 2;
51 if (order < MEMORY_PAGE_MINIMAL_ORDER)
52 return allocate_small_block (order);
53 if (order < MEMORY_PAGE_MAXIMAL_ORDER)
54 return memory_allocate_page (order);
55 return MEMORY_RETURN_FAILURE;
56 }
57
58static inline int release_block (int order,void *address)
59 {
60 struct memory_cache *cache = free_block_cache[order - 2];
61 if (cache)
62 return memory_cache_release (cache,address);
63 return MEMORY_RETURN_FAILURE;
64 }
65
66int memory_release_block (int order,void *address)
67 {
68 if (order < 2)
69 order = 2;
70 if (order < MEMORY_PAGE_MINIMAL_ORDER)
71 return release_block (order);
72 if (order < MEMORY_PAGE_MAXIMAL_ORDER)
73 return memory_release_page (address);
74 return MEMORY_RETURN_FAILURE;
75 }
76
77#endif