agileRTOS (zrtos)  Version 0.8.0 (ghostbuster)
malloc_limit.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 ykat UG (haftungsbeschraenkt) - All Rights Reserved
3  *
4  * Permission for non-commercial use is hereby granted,
5  * free of charge, without warranty of any kind.
6  */
7 #ifndef ZRTOS_MALLOC_LIMIT_H
8 #define ZRTOS_MALLOC_LIMIT_H
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #include <zrtos/malloc.h>
14 
15 #ifdef ZRTOS_MALLOC__CFG_DISABLE_FREE
16 #error "ZRTOS_MALLOC__CFG_DISABLE_FREE not supported"
17 #endif
18 
19 typedef struct _zrtos_malloc_limit_t{
20  size_t length;
22 
23 #define ZRTOS_MALLOC_LIMIT__INIT() {.length = 0}
24 
27  ,size_t length
28  ,size_t limit
29 ){
30  void *ret = 0;
31  size_t len = thiz->length + length;
32  if(len <= limit){
33  ret = kmalloc(length);
34  if(ret){
35  thiz->length = len;
36  }
37  }
38  return ret;
39 }
40 
42  thiz->length -= zrtos_malloc__get_length(ptr);
43  kfree(ptr);
44 }
45 
46 
47 #ifdef __cplusplus
48 }
49 #endif
50 #endif
void zrtos_malloc_limit__free(zrtos_malloc_limit_t *thiz, void *ptr)
Definition: malloc_limit.h:41
size_t zrtos_malloc__get_length(void *ptr)
Definition: malloc.h:205
struct _zrtos_malloc_limit_t zrtos_malloc_limit_t
void * zrtos_malloc_limit__malloc(zrtos_malloc_limit_t *thiz, size_t length, size_t limit)
Definition: malloc_limit.h:25