/src/libiec61850/hal/memory/lib_memory.c
Line | Count | Source |
1 | | /* |
2 | | * lib_memory.c |
3 | | * |
4 | | * Copyright 2014-2021 Michael Zillgith |
5 | | * |
6 | | * This file is part of Platform Abstraction Layer (libpal) |
7 | | * for libiec61850, libmms, and lib60870. |
8 | | */ |
9 | | |
10 | | #include <stdlib.h> |
11 | | #include "lib_memory.h" |
12 | | |
13 | | static MemoryExceptionHandler exceptionHandler = NULL; |
14 | | static void* exceptionHandlerParameter = NULL; |
15 | | |
16 | | static void |
17 | | noMemoryAvailableHandler(void) |
18 | 0 | { |
19 | 0 | if (exceptionHandler != NULL) |
20 | 0 | exceptionHandler(exceptionHandlerParameter); |
21 | 0 | } |
22 | | |
23 | | void |
24 | | Memory_installExceptionHandler(MemoryExceptionHandler handler, void* parameter) |
25 | 0 | { |
26 | 0 | exceptionHandler = handler; |
27 | 0 | exceptionHandlerParameter = parameter; |
28 | 0 | } |
29 | | |
30 | | void* |
31 | | Memory_malloc(size_t size) |
32 | 164k | { |
33 | 164k | void* memory = malloc(size); |
34 | | |
35 | 164k | if (memory == NULL) |
36 | 0 | noMemoryAvailableHandler(); |
37 | | |
38 | 164k | return memory; |
39 | 164k | } |
40 | | |
41 | | void* |
42 | | Memory_calloc(size_t nmemb, size_t size) |
43 | 1.71M | { |
44 | 1.71M | void* memory = calloc(nmemb, size); |
45 | | |
46 | 1.71M | if (memory == NULL) |
47 | 0 | noMemoryAvailableHandler(); |
48 | | |
49 | 1.71M | return memory; |
50 | 1.71M | } |
51 | | |
52 | | void * |
53 | | Memory_realloc(void *ptr, size_t size) |
54 | 0 | { |
55 | 0 | void* memory = realloc(ptr, size); |
56 | |
|
57 | 0 | if (memory == NULL) |
58 | 0 | noMemoryAvailableHandler(); |
59 | |
|
60 | 0 | return memory; |
61 | 0 | } |
62 | | |
63 | | void |
64 | | Memory_free(void* memb) |
65 | 1.88M | { |
66 | 1.88M | free(memb); |
67 | 1.88M | } |
68 | | |