LispBM
lbm_memory.h
Go to the documentation of this file.
1 
2 /*
3  Copyright 2020, 2022 Joel Svensson svenssonjoel@yahoo.se
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /*
20  Motivation: Memory manager for allocation of strings and arrays
21  that will not be be located on the lisp-heap. These kinds of values
22  have thus far been allocated using the "malloc" function provided
23  on the platform. Using malloc is something I want to move away from
24  doing within the guts of lispBM as I want it to be possible on
25  running on the bare metal.
26 
27  ** This is already done!
28  Later perhaps things such as the symbol table with symbol mappings
29  should also be located on this managed memory area. Symbols,
30  however, are never freed after being created in lispBM. Currently I
31  dont know if that is a good idea? or if it is possible to free
32  unused symbols at all. To become entirely free from malloc, the entire
33  symbol table management must be rewritten.
34 */
35 
36 /*
37  Notes:
38  64Bytes = 16 * 4Byte words
39  4Bytes = 32Bits = 16 * 2Bit of status
40 
41  Status Bitpatterns:
42  00 - FREE or USED 4Byte word
43  01 - END of a sequence of allocated words
44  10 - START of a sequence of allocated words
45  11 - START and END of a sequence of allocated words (a 1 word allocation)
46 
47  0 1 2 3 4 5 6 7 8 9
48  [11 00 00 00 00 10 01 11 00 00]
49 
50  Favours allocation of small amounts of data.
51 
52  Requirements:
53  - Memory space is a multiple of 64Bytes.
54  - Memory status bitmap is the same multiple of 4Bytes.
55 
56  Number of bits in an offset from the base_address
57  MEMORY_SIZE_512 => 9
58  MEMORY_SIZE_1K => 10
59  MEMORY_SIZE_2K => 11
60  MEMORY_SIZE_1M => 20
61  MEMORY_SIZE_16M => 24
62  MEMORY_SIZE_32M => 25
63  MEMORY_SIZE_64M => 26
64  MEMORY_SIZE_128M => 27
65  MEMORY_SIZE_256M => 28
66 
67  However, due to alignment on a address multiple of 4, the 2 least
68  significant bits are zeroes. So an offset into memory of size up to
69  1GB should be possible to represent within a lispBM VALUE. This that
70  using the offset into memory could be used as the identity of a
71  symbol when it comes to replacing the symbol table.
72 
73 */
74 #ifndef _LISPBM_MEMORY_H_
75 #define _LISPBM_MEMORY_H_
76 
77 #include "lbm_types.h"
78 #include <stdint.h>
79 #include <stddef.h>
80 
81 #ifdef __cplusplus
82 extern "C" {
83 #endif
84 
85 #define LBM_MEMORY_SIZE_64BYTES_TIMES_X(X) (16*(X))
86 #ifndef LBM64
87 #define LBM_MEMORY_BITMAP_SIZE(X) (X)
88 #else
89 #define LBM_MEMORY_BITMAP_SIZE(X) ((X)/2)
90 #endif
91 
92 #define LBM_MEMORY_SIZE_512 LBM_MEMORY_SIZE_64BYTES_TIMES_X(8)
93 #define LBM_MEMORY_SIZE_1K LBM_MEMORY_SIZE_64BYTES_TIMES_X(16)
94 #define LBM_MEMORY_SIZE_2K LBM_MEMORY_SIZE_64BYTES_TIMES_X(32)
95 #define LBM_MEMORY_SIZE_4K LBM_MEMORY_SIZE_64BYTES_TIMES_X(64)
96 #define LBM_MEMORY_SIZE_8K LBM_MEMORY_SIZE_64BYTES_TIMES_X(128)
97 #define LBM_MEMORY_SIZE_10K LBM_MEMORY_SIZE_64BYTES_TIMES_X(160)
98 #define LBM_MEMORY_SIZE_12K LBM_MEMORY_SIZE_64BYTES_TIMES_X(192)
99 #define LBM_MEMORY_SIZE_14K LBM_MEMORY_SIZE_64BYTES_TIMES_X(224)
100 #define LBM_MEMORY_SIZE_16K LBM_MEMORY_SIZE_64BYTES_TIMES_X(256)
101 #define LBM_MEMORY_SIZE_32K LBM_MEMORY_SIZE_64BYTES_TIMES_X(512)
102 #define LBM_MEMORY_SIZE_1M LBM_MEMORY_SIZE_64BYTES_TIMES_X(16384)
103 
104 #define LBM_MEMORY_BITMAP_SIZE_512 LBM_MEMORY_BITMAP_SIZE(8)
105 #define LBM_MEMORY_BITMAP_SIZE_1K LBM_MEMORY_BITMAP_SIZE(16)
106 #define LBM_MEMORY_BITMAP_SIZE_2K LBM_MEMORY_BITMAP_SIZE(32)
107 #define LBM_MEMORY_BITMAP_SIZE_4K LBM_MEMORY_BITMAP_SIZE(64)
108 #define LBM_MEMORY_BITMAP_SIZE_8K LBM_MEMORY_BITMAP_SIZE(128)
109 #define LBM_MEMORY_BITMAP_SIZE_10K LBM_MEMORY_BITMAP_SIZE(160)
110 #define LBM_MEMORY_BITMAP_SIZE_12K LBM_MEMORY_BITMAP_SIZE(192)
111 #define LBM_MEMORY_BITMAP_SIZE_14K LBM_MEMORY_BITMAP_SIZE(224)
112 #define LBM_MEMORY_BITMAP_SIZE_16K LBM_MEMORY_BITMAP_SIZE(256)
113 #define LBM_MEMORY_BITMAP_SIZE_32K LBM_MEMORY_BITMAP_SIZE(512)
114 #define LBM_MEMORY_BITMAP_SIZE_1M LBM_MEMORY_BITMAP_SIZE(16384)
115 
124 int lbm_memory_init(lbm_uint *data, lbm_uint data_size,
126 
130 void lbm_memory_set_reserve(lbm_uint num_words);
160 int lbm_memory_free(lbm_uint *ptr);
165 void* lbm_malloc(size_t size);
170 void* lbm_malloc_reserve(size_t size);
174 void lbm_free(void *ptr);
180 int lbm_memory_shrink(lbm_uint *ptr, lbm_uint n);
181 
188 
189 
191 
192 #ifdef __cplusplus
193 }
194 #endif
195 #endif
lbm_free
void lbm_free(void *ptr)
Definition: lbm_memory.c:412
lbm_malloc_reserve
void * lbm_malloc_reserve(size_t size)
Definition: lbm_memory.c:399
lbm_memory_address_to_ix
lbm_int lbm_memory_address_to_ix(lbm_uint *ptr)
Definition: lbm_memory.c:112
lbm_uint
uint32_t lbm_uint
Definition: lbm_types.h:48
lbm_malloc
void * lbm_malloc(size_t size)
Definition: lbm_memory.c:385
lbm_memory_num_words
lbm_uint lbm_memory_num_words(void)
Definition: lbm_memory.c:159
lbm_memory_shrink
int lbm_memory_shrink(lbm_uint *ptr, lbm_uint n)
Definition: lbm_memory.c:416
lbm_memory_ptr_inside
int lbm_memory_ptr_inside(lbm_uint *ptr)
Definition: lbm_memory.c:473
bitmap
static lbm_uint * bitmap
Definition: lbm_memory.c:42
lbm_memory_init
int lbm_memory_init(lbm_uint *data, lbm_uint data_size, lbm_uint *bitmap, lbm_uint bitmap_size)
Definition: lbm_memory.c:53
lbm_memory_allocate
lbm_uint * lbm_memory_allocate(lbm_uint num_words)
Definition: lbm_memory.c:338
lbm_memory_num_free
lbm_uint lbm_memory_num_free(void)
Definition: lbm_memory.c:163
lbm_types.h
lbm_memory_free
int lbm_memory_free(lbm_uint *ptr)
Definition: lbm_memory.c:346
lbm_int
int32_t lbm_int
Definition: lbm_types.h:49
lbm_memory_longest_free
lbm_uint lbm_memory_longest_free(void)
Definition: lbm_memory.c:207
bitmap_size
static lbm_uint bitmap_size
Definition: lbm_memory.c:45
lbm_memory_set_reserve
void lbm_memory_set_reserve(lbm_uint num_words)
Definition: lbm_memory.c:96
lbm_memory_get_reserve
lbm_uint lbm_memory_get_reserve(void)
Definition: lbm_memory.c:100