malloc.c 2.42 KB
Newer Older
Damien's avatar
Damien committed
1
2
3
4
#include <stdio.h>
#include <stdlib.h>

#include "misc.h"
5
#include "mpconfig.h"
Damien's avatar
Damien committed
6

7
#if MICROPY_MEM_STATS
Damien's avatar
Damien committed
8
static int total_bytes_allocated = 0;
9
static int current_bytes_allocated = 0;
10
11
12
static int peak_bytes_allocated = 0;

#define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; }
13
#endif
Damien's avatar
Damien committed
14
15
16
17
18
19
20
21
22
23

void *m_malloc(int num_bytes) {
    if (num_bytes == 0) {
        return NULL;
    }
    void *ptr = malloc(num_bytes);
    if (ptr == NULL) {
        printf("could not allocate memory, allocating %d bytes\n", num_bytes);
        return NULL;
    }
24
#if MICROPY_MEM_STATS
Damien's avatar
Damien committed
25
    total_bytes_allocated += num_bytes;
26
    current_bytes_allocated += num_bytes;
27
    UPDATE_PEAK();
28
#endif
Damien's avatar
Damien committed
29
30
31
32
33
34
35
36
37
38
39
40
    return ptr;
}

void *m_malloc0(int num_bytes) {
    if (num_bytes == 0) {
        return NULL;
    }
    void *ptr = calloc(1, num_bytes);
    if (ptr == NULL) {
        printf("could not allocate memory, allocating %d bytes\n", num_bytes);
        return NULL;
    }
41
#if MICROPY_MEM_STATS
Damien's avatar
Damien committed
42
    total_bytes_allocated += num_bytes;
43
    current_bytes_allocated += num_bytes;
44
    UPDATE_PEAK();
45
#endif
Damien's avatar
Damien committed
46
47
48
    return ptr;
}

49
50
void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
    if (new_num_bytes == 0) {
Damien's avatar
Damien committed
51
52
53
        free(ptr);
        return NULL;
    }
54
    ptr = realloc(ptr, new_num_bytes);
Damien's avatar
Damien committed
55
    if (ptr == NULL) {
56
        printf("could not allocate memory, reallocating %d bytes\n", new_num_bytes);
Damien's avatar
Damien committed
57
58
        return NULL;
    }
59
#if MICROPY_MEM_STATS
60
61
62
63
64
    // At first thought, "Total bytes allocated" should only grow,
    // after all, it's *total*. But consider for example 2K block
    // shrunk to 1K and then grown to 2K again. It's still 2K
    // allocated total. If we process only positive increments,
    // we'll count 3K.
65
66
67
    int diff = new_num_bytes - old_num_bytes;
    total_bytes_allocated += diff;
    current_bytes_allocated += diff;
68
    UPDATE_PEAK();
69
#endif
Damien's avatar
Damien committed
70
71
72
    return ptr;
}

73
74
75
76
void m_free(void *ptr, int num_bytes) {
    if (ptr != NULL) {
        free(ptr);
    }
77
#if MICROPY_MEM_STATS
78
    current_bytes_allocated -= num_bytes;
79
#endif
80
81
}

82
int m_get_total_bytes_allocated(void) {
83
#if MICROPY_MEM_STATS
Damien's avatar
Damien committed
84
    return total_bytes_allocated;
85
86
87
#else
    return -1;
#endif
Damien's avatar
Damien committed
88
}
89
90

int m_get_current_bytes_allocated(void) {
91
#if MICROPY_MEM_STATS
92
    return current_bytes_allocated;
93
94
95
#else
    return -1;
#endif
96
}
97
98

int m_get_peak_bytes_allocated(void) {
99
#if MICROPY_MEM_STATS
100
    return peak_bytes_allocated;
101
102
103
#else
    return -1;
#endif
104
}