blob: 4f01dc63f5208ccba6678b90fbf5f6a9999b4367 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdio.h>
2#include <stdlib.h>
3
4#include "misc.h"
Paul Sokolovskyef181022014-01-03 03:06:25 +02005#include "mpconfig.h"
Damien429d7192013-10-04 19:53:11 +01006
Paul Sokolovskyef181022014-01-03 03:06:25 +02007#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +01008static int total_bytes_allocated = 0;
Paul Sokolovsky02de0c52014-01-01 23:15:47 +02009static int current_bytes_allocated = 0;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020010static int peak_bytes_allocated = 0;
11
12#define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; }
Paul Sokolovskyef181022014-01-03 03:06:25 +020013#endif
Damien429d7192013-10-04 19:53:11 +010014
Damien429d7192013-10-04 19:53:11 +010015void *m_malloc(int num_bytes) {
16 if (num_bytes == 0) {
17 return NULL;
18 }
19 void *ptr = malloc(num_bytes);
20 if (ptr == NULL) {
21 printf("could not allocate memory, allocating %d bytes\n", num_bytes);
22 return NULL;
23 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020024#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +010025 total_bytes_allocated += num_bytes;
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020026 current_bytes_allocated += num_bytes;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020027 UPDATE_PEAK();
Paul Sokolovskyef181022014-01-03 03:06:25 +020028#endif
Damien429d7192013-10-04 19:53:11 +010029 return ptr;
30}
31
32void *m_malloc0(int num_bytes) {
33 if (num_bytes == 0) {
34 return NULL;
35 }
36 void *ptr = calloc(1, num_bytes);
37 if (ptr == NULL) {
38 printf("could not allocate memory, allocating %d bytes\n", num_bytes);
39 return NULL;
40 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020041#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +010042 total_bytes_allocated += num_bytes;
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020043 current_bytes_allocated += num_bytes;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020044 UPDATE_PEAK();
Paul Sokolovskyef181022014-01-03 03:06:25 +020045#endif
Damien429d7192013-10-04 19:53:11 +010046 return ptr;
47}
48
Damien732407f2013-12-29 19:33:23 +000049void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
50 if (new_num_bytes == 0) {
Damien429d7192013-10-04 19:53:11 +010051 free(ptr);
52 return NULL;
53 }
Damien732407f2013-12-29 19:33:23 +000054 ptr = realloc(ptr, new_num_bytes);
Damien429d7192013-10-04 19:53:11 +010055 if (ptr == NULL) {
Damien732407f2013-12-29 19:33:23 +000056 printf("could not allocate memory, reallocating %d bytes\n", new_num_bytes);
Damien429d7192013-10-04 19:53:11 +010057 return NULL;
58 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020059#if MICROPY_MEM_STATS
Paul Sokolovsky43f1c802014-01-01 23:04:25 +020060 // At first thought, "Total bytes allocated" should only grow,
61 // after all, it's *total*. But consider for example 2K block
62 // shrunk to 1K and then grown to 2K again. It's still 2K
63 // allocated total. If we process only positive increments,
64 // we'll count 3K.
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020065 int diff = new_num_bytes - old_num_bytes;
66 total_bytes_allocated += diff;
67 current_bytes_allocated += diff;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020068 UPDATE_PEAK();
Paul Sokolovskyef181022014-01-03 03:06:25 +020069#endif
Damien429d7192013-10-04 19:53:11 +010070 return ptr;
71}
72
Damien732407f2013-12-29 19:33:23 +000073void m_free(void *ptr, int num_bytes) {
74 if (ptr != NULL) {
75 free(ptr);
76 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020077#if MICROPY_MEM_STATS
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020078 current_bytes_allocated -= num_bytes;
Paul Sokolovskyef181022014-01-03 03:06:25 +020079#endif
Damien732407f2013-12-29 19:33:23 +000080}
81
Damien8b3a7c22013-10-23 20:20:17 +010082int m_get_total_bytes_allocated(void) {
Paul Sokolovskyef181022014-01-03 03:06:25 +020083#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +010084 return total_bytes_allocated;
Paul Sokolovskyef181022014-01-03 03:06:25 +020085#else
86 return -1;
87#endif
Damien429d7192013-10-04 19:53:11 +010088}
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020089
90int m_get_current_bytes_allocated(void) {
Paul Sokolovskyef181022014-01-03 03:06:25 +020091#if MICROPY_MEM_STATS
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020092 return current_bytes_allocated;
Paul Sokolovskyef181022014-01-03 03:06:25 +020093#else
94 return -1;
95#endif
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020096}
Paul Sokolovsky780f5552014-01-01 23:42:21 +020097
98int m_get_peak_bytes_allocated(void) {
Paul Sokolovskyef181022014-01-03 03:06:25 +020099#if MICROPY_MEM_STATS
Paul Sokolovsky780f5552014-01-01 23:42:21 +0200100 return peak_bytes_allocated;
Paul Sokolovskyef181022014-01-03 03:06:25 +0200101#else
102 return -1;
103#endif
Paul Sokolovsky780f5552014-01-01 23:42:21 +0200104}