XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
allocator.c
浏览该文件的文档.
1
13#include <xwos/standard.h>
14#include <string.h>
15#include <xwos/lib/xwbop.h>
16#include <xwos/mm/common.h>
19
20/* #define XWMM_MEMPOOLLOGF */
21#ifdef XWMM_MEMPOOLLOGF
22# define xwmm_mempoollogf(lv, thd, fmt, ...) \
23 xwlogf(lv, "Thd:%s", fmt, thd->stack.name, ##__VA_ARGS__)
24#else
25# define xwmm_mempoollogf(lv, thd, fmt, ...)
26#endif
27
41static __xwos_code
42xwer_t xwmm_mempool_construct(struct xwmm_mempool * mp, const char * name,
43 xwptr_t origin, xwsz_t size,
44 struct xwmm_mempool_page_odrbtree * odrbtree,
45 struct xwmm_mempool_page * pgarray)
46{
47 xwer_t rc;
48
50 origin, size, XWMM_MEMPOOL_PAGE_SIZE,
51 odrbtree, pgarray);
52 if (rc < 0) {
53 goto err_pa_init;
54 }
55
56 rc = xwmm_mempool_objcache_init(&mp->oc_8, &mp->pa, NULL,
59 NULL, NULL);
60 if (rc < 0) {
61 goto err_oc_8_init;
62 }
63
64 rc = xwmm_mempool_objcache_init(&mp->oc_16, &mp->pa, NULL,
67 NULL, NULL);
68 if (rc < 0) {
69 goto err_oc_16_init;
70 }
71
72 rc = xwmm_mempool_objcache_init(&mp->oc_32, &mp->pa, NULL,
75 NULL, NULL);
76 if (rc < 0) {
77 goto err_oc_32_init;
78 }
79
80 rc = xwmm_mempool_objcache_init(&mp->oc_64, &mp->pa, NULL,
83 NULL, NULL);
84 if (rc < 0) {
85 goto err_oc_64_init;
86 }
87
88 rc = xwmm_mempool_objcache_init(&mp->oc_96, &mp->pa, NULL,
91 NULL, NULL);
92 if (rc < 0) {
93 goto err_oc_96_init;
94 }
95
97 128, XWMM_ALIGNMENT,
99 NULL, NULL);
100 if (rc < 0) {
101 goto err_oc_128_init;
102 }
103
104 rc = xwmm_mempool_objcache_init(&mp->oc_160, &mp->pa, NULL,
105 160, XWMM_ALIGNMENT,
107 NULL, NULL);
108 if (rc < 0) {
109 goto err_oc_160_init;
110 }
111
112 rc = xwmm_mempool_objcache_init(&mp->oc_192, &mp->pa, NULL,
113 192, XWMM_ALIGNMENT,
115 NULL, NULL);
116 if (rc < 0) {
117 goto err_oc_192_init;
118 }
119
120 rc = xwmm_mempool_objcache_init(&mp->oc_256, &mp->pa, NULL,
121 256, XWMM_ALIGNMENT,
123 NULL, NULL);
124 if (rc < 0) {
125 goto err_oc_256_init;
126 }
127
128 rc = xwmm_mempool_objcache_init(&mp->oc_320, &mp->pa, NULL,
129 320, XWMM_ALIGNMENT,
131 NULL, NULL);
132 if (rc < 0) {
133 goto err_oc_320_init;
134 }
135
136 rc = xwmm_mempool_objcache_init(&mp->oc_384, &mp->pa, NULL,
137 384, XWMM_ALIGNMENT,
139 NULL, NULL);
140 if (rc < 0) {
141 goto err_oc_384_init;
142 }
143
144 rc = xwmm_mempool_objcache_init(&mp->oc_512, &mp->pa, NULL,
145 512, XWMM_ALIGNMENT,
147 NULL, NULL);
148 if (rc < 0) {
149 goto err_oc_512_init;
150 }
151
152 rc = xwmm_mempool_objcache_init(&mp->oc_768, &mp->pa, NULL,
153 768, XWMM_ALIGNMENT,
155 NULL, NULL);
156 if (rc < 0) {
157 goto err_oc_768_init;
158 }
159
160 rc = xwmm_mempool_objcache_init(&mp->oc_1024, &mp->pa, NULL,
161 1024, XWMM_ALIGNMENT,
163 NULL, NULL);
164 if (rc < 0) {
165 goto err_oc_1024_init;
166 }
167
168 rc = xwmm_mempool_objcache_init(&mp->oc_2048, &mp->pa, NULL,
169 2048, XWMM_ALIGNMENT,
171 NULL, NULL);
172 if (rc < 0) {
173 goto err_oc_2048_init;
174 }
175
176 mp->name = name;
177
178 return XWOK;
179
180err_oc_2048_init:
181err_oc_1024_init:
182err_oc_768_init:
183err_oc_512_init:
184err_oc_384_init:
185err_oc_320_init:
186err_oc_256_init:
187err_oc_192_init:
188err_oc_160_init:
189err_oc_128_init:
190err_oc_96_init:
191err_oc_64_init:
192err_oc_32_init:
193err_oc_16_init:
194err_oc_8_init:
195err_pa_init:
196 return rc;
197}
198
200xwer_t xwmm_mempool_init(struct xwmm_mempool * mp, const char * name,
201 xwptr_t origin, xwsz_t size, xwsz_t pgodr,
202 xwsz_t pre, void ** membuf)
203{
204 xwer_t rc;
205 struct xwmm_mempool_page_odrbtree * odrbtrees;
206 xwsz_t odrbtrees_size;
207 struct xwmm_mempool_page * pages;
208 xwsz_t pages_nr;
209 xwsz_t pages_size;
210 void * mem;
211
212 XWOS_VALIDATE((mp), "nullptr", -EFAULT);
213
214 pages_nr = size / XWMM_MEMPOOL_PAGE_SIZE;
215 if (pages_nr != (1U << pgodr)) {
216 rc = - ESIZE;
217 goto err_size;
218 }
219
220 odrbtrees = (struct xwmm_mempool_page_odrbtree *)&mp[(xwsz_t)1];
221 pages = (struct xwmm_mempool_page *)&odrbtrees[pgodr + (xwsz_t)1];
222 odrbtrees_size = sizeof(struct xwmm_mempool_page_odrbtree) *
223 (pgodr + (xwsz_t)1);
224 pages_size = sizeof(struct xwmm_mempool_page) << pgodr;
225 rc = xwmm_mempool_construct(mp, name, origin, size, odrbtrees, pages);
226 if (rc < 0) {
227 goto err_mempool_construct;
228 }
229
230 if (pre > (xwsz_t)0) {
231 rc = xwmm_mempool_malloc(mp, pre, &mem);
232 if (rc < 0) {
233 goto err_mempool_prealloc;
234 }
235 if (NULL != membuf) {
236 *membuf = mem;
237 }
238 } else if ((xwptr_t)mp == origin) {
239 xwsz_t mpsz = sizeof(struct xwmm_mempool) + odrbtrees_size + pages_size;
240 if (mpsz < XWMM_MEMPOOL_PAGE_SIZE) {
242 }
243 rc = xwmm_mempool_malloc(mp, mpsz, &mem);
244 if (rc < 0) {
245 goto err_mempool_prealloc;
246 }
247 if (NULL != membuf) {
248 *membuf = mem;
249 }
250 } else {}
251 return XWOK;
252
253err_mempool_prealloc:
254err_mempool_construct:
255err_size:
256 return rc;
257}
258
260xwer_t xwmm_mempool_malloc(struct xwmm_mempool * mp, xwsz_t size, void ** membuf)
261{
262 interface xwmm_mempool_i_allocator * ia;
263 xwssq_t odr;
264 xwer_t rc;
265
266 XWOS_VALIDATE((mp), "nullptr", -EFAULT);
267 XWOS_VALIDATE((membuf), "nullptr", -EFAULT);
268
269 if ((xwsz_t)0 == size) {
270 rc = XWOK;
271 *membuf = NULL;
272 } else {
273 size = XWBOP_ALIGN(size, (xwsz_t)XWMM_ALIGNMENT);
274 odr = xwbop_fls(xwsz_t, size);
275 while (((xwsz_t)1 << (xwsz_t)odr) < size) {
276 odr++;
277 }
278 // cppcheck-suppress [misra-c2012-17.8]
279 size = (xwsz_t)1 << (xwsz_t)odr;
280
281 switch (size) {
282 case 8:
283 ia = (void *)&mp->oc_8;
284 break;
285 case 16:
286 ia = (void *)&mp->oc_16;
287 break;
288 case 32:
289 ia = (void *)&mp->oc_32;
290 break;
291 case 64:
292 ia = (void *)&mp->oc_64;
293 break;
294 case 128:
295 if (size <= (xwsz_t)96) {
296 ia = (void *)&mp->oc_96;
297 } else {
298 ia = (void *)&mp->oc_128;
299 }
300 break;
301 case 256:
302 if (size <= (xwsz_t)160) {
303 ia = (void *)&mp->oc_160;
304 } else if (size <= (xwsz_t)192) {
305 ia = (void *)&mp->oc_192;
306 } else {
307 ia = (void *)&mp->oc_256;
308 }
309 break;
310 case 512:
311 if (size <= (xwsz_t)320) {
312 ia = (void *)&mp->oc_320;
313 } else if (size <= (xwsz_t)384) {
314 ia = (void *)&mp->oc_384;
315 } else {
316 ia = (void *)&mp->oc_512;
317 }
318 break;
319 case 1024:
320 if (size <= (xwsz_t)768) {
321 ia = (void *)&mp->oc_768;
322 } else {
323 ia = (void *)&mp->oc_1024;
324 }
325 break;
326 case 2048:
327 ia = (void *)&mp->oc_2048;
328 break;
329 default:
330 ia = (void *)&mp->pa;
331 break;
332 }
333 rc = ia->malloc(ia, size, membuf);
334 if (rc < 0) {
335 *membuf = NULL;
336 }
337 }
338 return rc;
339}
340
342xwer_t xwmm_mempool_free(struct xwmm_mempool * mp, void * mem)
343{
344 interface xwmm_mempool_i_allocator * ia;
345 struct xwmm_mempool_page * pg;
346 xwer_t rc;
347
348 XWOS_VALIDATE((mp), "nullptr", -EFAULT);
349
350 if (NULL == mem) {
351 rc = XWOK;
352 goto do_nothing;
353 }
354
355 rc = xwmm_mempool_page_find(&mp->pa, mem, &pg);
356 if (rc < 0) {
357 goto err_pg_find;
358 }
359
360 switch (pg->data.value) {
361 case 8:
362 ia = (void *)&mp->oc_8;
363 break;
364 case 16:
365 ia = (void *)&mp->oc_16;
366 break;
367 case 32:
368 ia = (void *)&mp->oc_32;
369 break;
370 case 64:
371 ia = (void *)&mp->oc_64;
372 break;
373 case 96:
374 ia = (void *)&mp->oc_96;
375 break;
376 case 128:
377 ia = (void *)&mp->oc_128;
378 break;
379 case 160:
380 ia = (void *)&mp->oc_160;
381 break;
382 case 192:
383 ia = (void *)&mp->oc_192;
384 break;
385 case 256:
386 ia = (void *)&mp->oc_256;
387 break;
388 case 320:
389 ia = (void *)&mp->oc_320;
390 break;
391 case 384:
392 ia = (void *)&mp->oc_384;
393 break;
394 case 512:
395 ia = (void *)&mp->oc_512;
396 break;
397 case 768:
398 ia = (void *)&mp->oc_768;
399 break;
400 case 1024:
401 ia = (void *)&mp->oc_1024;
402 break;
403 case 2048:
404 ia = (void *)&mp->oc_2048;
405 break;
406 default:
407 ia = (void *)&mp->pa;
408 break;
409 }
410 rc = ia->free(ia, mem);
411
412err_pg_find:
413do_nothing:
414 return rc;
415}
416
418xwer_t xwmm_mempool_realloc(struct xwmm_mempool * mp, xwsz_t size, void ** membuf)
419{
420 xwer_t rc;
421
422 XWOS_VALIDATE((mp), "nullptr", -EFAULT);
423 XWOS_VALIDATE((membuf), "nullptr", -EFAULT);
424
425 if (NULL == *membuf) {
426 rc = xwmm_mempool_malloc(mp, size, membuf);
427 } else if ((xwsz_t)0 == size) {
428 rc = xwmm_mempool_free(mp, *membuf);
429 if (XWOK == rc) {
430 *membuf = NULL;
431 }
432 } else {
433 struct xwmm_mempool_page * pg;
434 void * oldmem;
435 void * newmem;
436
437 oldmem = *membuf;
438 rc = xwmm_mempool_page_find(&mp->pa, oldmem, &pg);
439 if (XWOK == rc) {
440 if (size <= pg->data.value) {
441 rc = XWOK;
442 } else {
443 rc = xwmm_mempool_malloc(mp, size, &newmem);
444 if (XWOK == rc) {
445 // cppcheck-suppress [misra-c2012-17.7]
446 memcpy(newmem, oldmem, pg->data.value);
447 rc = xwmm_mempool_free(mp, oldmem);
448 if (XWOK == rc) {
449 *membuf = newmem;
450 } else {
451 // cppcheck-suppress [misra-c2012-17.7]
452 xwmm_mempool_free(mp, newmem);
453 *membuf = NULL;
454 }
455 } else {
456 *membuf = NULL;
457 }
458 }
459 } else {
460 *membuf = NULL;
461 }
462 }
463 return rc;
464}
465
468 xwsz_t alignment, xwsz_t size,
469 void ** membuf)
470{
471 interface xwmm_mempool_i_allocator * ia;
472 xwssq_t p2;
473 xwer_t rc;
474
475 XWOS_VALIDATE((mp), "nullptr", -EFAULT);
476 XWOS_VALIDATE((membuf), "nullptr", -EFAULT);
477
478 if (alignment < XWMM_ALIGNMENT) {
479 alignment = XWMM_ALIGNMENT;
480 }
481 p2 = xwbop_fls(xwsz_t, alignment);
482 if (((xwsz_t)1 << (xwsz_t)p2) != alignment) {
483 rc = -EINVAL;
484 goto err_notp2;
485 }
486 if ((xwsz_t)0 == size) {
487 rc = XWOK;
488 *membuf = NULL;
489 goto nothing;
490 }
491 if (size <= alignment) {
492 size = alignment;
493 } else {
494 p2 = xwbop_fls(xwsz_t, size);
495 while (((xwsz_t)1 << (xwsz_t)p2) < size) {
496 p2++;
497 }
498 size = (xwsz_t)1 << (xwsz_t)p2;
499 }
500
501 switch (size) {
502 case 8:
503 ia = (void *)&mp->oc_8;
504 break;
505 case 16:
506 ia = (void *)&mp->oc_16;
507 break;
508 case 32:
509 ia = (void *)&mp->oc_32;
510 break;
511 case 64:
512 ia = (void *)&mp->oc_64;
513 break;
514 case 128:
515 ia = (void *)&mp->oc_128;
516 break;
517 case 256:
518 ia = (void *)&mp->oc_256;
519 break;
520 case 512:
521 ia = (void *)&mp->oc_512;
522 break;
523 case 1024:
524 ia = (void *)&mp->oc_1024;
525 break;
526 case 2048:
527 ia = (void *)&mp->oc_2048;
528 break;
529 default:
530 ia = (void *)&mp->pa;
531 break;
532 }
533 rc = ia->malloc(ia, size, membuf);
534
535nothing:
536err_notp2:
537 return rc;
538}
static xwer_t xwmm_mempool_construct(struct xwmm_mempool *mp, const char *name, xwptr_t origin, xwsz_t size, struct xwmm_mempool_page_odrbtree *odrbtree, struct xwmm_mempool_page *pgarray)
构建内存池
Definition allocator.c:42
XWOS内存管理:内存池:页分配器
#define XWMM_ALIGNMENT
Definition common.h:29
xwer_t xwmm_mempool_objcache_init(struct xwmm_mempool_objcache *oc, struct xwmm_mempool_page_allocator *pa, const char *name, xwsz_t objsize, xwsz_t alignment, xwsq_t pg_order, ctor_f ctor, dtor_f dtor)
XWMM API:初始化对象缓存
Definition objcache.c:54
#define XWMM_MEMPOOL_OC_384_PAGE_ODR
Definition allocator.h:62
#define XWMM_MEMPOOL_OC_128_PAGE_ODR
Definition allocator.h:57
#define XWMM_MEMPOOL_OC_160_PAGE_ODR
Definition allocator.h:58
#define XWMM_MEMPOOL_OC_320_PAGE_ODR
Definition allocator.h:61
#define XWMM_MEMPOOL_OC_256_PAGE_ODR
Definition allocator.h:60
#define XWMM_MEMPOOL_OC_16_PAGE_ODR
Definition allocator.h:53
xwer_t xwmm_mempool_malloc(struct xwmm_mempool *mp, xwsz_t size, void **membuf)
XWMM API:从内存池中申请内存
Definition allocator.c:260
#define XWMM_MEMPOOL_OC_64_PAGE_ODR
Definition allocator.h:55
xwer_t xwmm_mempool_memalign(struct xwmm_mempool *mp, xwsz_t alignment, xwsz_t size, void **membuf)
XWMM API:从内存池中申请对齐的内存
Definition allocator.c:467
xwer_t xwmm_mempool_init(struct xwmm_mempool *mp, const char *name, xwptr_t origin, xwsz_t size, xwsz_t pgodr, xwsz_t pre, void **membuf)
XWMM API:初始化内存池
Definition allocator.c:200
#define XWMM_MEMPOOL_OC_192_PAGE_ODR
Definition allocator.h:59
#define XWMM_MEMPOOL_OC_8_PAGE_ODR
Definition allocator.h:52
#define XWMM_MEMPOOL_OC_768_PAGE_ODR
Definition allocator.h:64
xwer_t xwmm_mempool_page_find(struct xwmm_mempool_page_allocator *pa, void *mem, struct xwmm_mempool_page **pgbuf)
依据内存地址查找页
Definition page.c:519
#define XWMM_MEMPOOL_OC_96_PAGE_ODR
Definition allocator.h:56
#define XWMM_MEMPOOL_PAGE_SIZE
Definition allocator.h:51
#define XWMM_MEMPOOL_OC_2048_PAGE_ODR
Definition allocator.h:66
#define XWMM_MEMPOOL_OC_32_PAGE_ODR
Definition allocator.h:54
xwer_t xwmm_mempool_free(struct xwmm_mempool *mp, void *mem)
XWMM API:释放内存
Definition allocator.c:342
xwer_t xwmm_mempool_page_allocator_init(struct xwmm_mempool_page_allocator *pa, const char *name, xwptr_t origin, xwsz_t size, xwsz_t pgsize, struct xwmm_mempool_page_odrbtree *odrbtree, struct xwmm_mempool_page *pgarray)
XWMM API:初始化页分配器
Definition page.c:86
#define XWMM_MEMPOOL_OC_512_PAGE_ODR
Definition allocator.h:63
#define XWMM_MEMPOOL_OC_1024_PAGE_ODR
Definition allocator.h:65
xwer_t xwmm_mempool_realloc(struct xwmm_mempool *mp, xwsz_t size, void **membuf)
XWMM API:调整内存大小
Definition allocator.c:418
#define __xwos_code
Definition compiler.h:171
#define __xwos_api
Definition compiler.h:175
#define EINVAL
Invalid argument
Definition errno.h:52
#define EFAULT
Bad address
Definition errno.h:44
#define XWOK
No error
Definition errno.h:182
#define ESIZE
Size error
Definition errno.h:199
signed long xwer_t
Definition type.h:554
#define NULL
Definition type.h:28
unsigned long xwsz_t
Definition type.h:339
signed long xwssq_t
Definition type.h:461
unsigned long xwptr_t
Definition type.h:375
#define xwbop_fls(type, data)
XWOS BOPLIB:在数据中从最高位起查找第一个被置1的位
Definition xwbop.h:169
#define XWBOP_ALIGN(x, n)
Definition xwbop.h:37
#define XWOS_VALIDATE(exp, errstr,...)
检查函数参数是否有效
Definition standard.h:76
XWOS内存管理:内存池:页分配器
interface:分配器
Definition i_allocator.h:27
xwer_t(* free)(void *, void *)
Definition i_allocator.h:31
xwer_t(* malloc)(void *, xwsz_t, void **)
Definition i_allocator.h:29
内存页
Definition page.h:37
xwsq_t value
Definition page.h:61
union xwmm_mempool_page::@2 data
内存池
Definition allocator.h:86
struct xwmm_mempool_page_allocator pa
Definition allocator.h:88
struct xwmm_mempool_objcache oc_512
Definition allocator.h:100
struct xwmm_mempool_objcache oc_32
Definition allocator.h:91
struct xwmm_mempool_objcache oc_1024
Definition allocator.h:102
struct xwmm_mempool_objcache oc_768
Definition allocator.h:101
struct xwmm_mempool_objcache oc_160
Definition allocator.h:95
struct xwmm_mempool_objcache oc_8
Definition allocator.h:89
struct xwmm_mempool_objcache oc_192
Definition allocator.h:96
struct xwmm_mempool_objcache oc_96
Definition allocator.h:93
struct xwmm_mempool_objcache oc_2048
Definition allocator.h:103
struct xwmm_mempool_objcache oc_256
Definition allocator.h:97
struct xwmm_mempool_objcache oc_128
Definition allocator.h:94
struct xwmm_mempool_objcache oc_384
Definition allocator.h:99
struct xwmm_mempool_objcache oc_320
Definition allocator.h:98
struct xwmm_mempool_objcache oc_64
Definition allocator.h:92
struct xwmm_mempool_objcache oc_16
Definition allocator.h:90
const char * name
Definition allocator.h:87
XWOS通用库:位操作
XWOS内存管理:通用定义
XWOS的标准头文件