blob: 59570243f01934f1934e374f73994c6e57d6a939 [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
Damien George2d15c122014-01-29 20:33:20 +00007#if 0 // print debugging info
8#define DEBUG_printf(args...) printf(args)
9#else // don't print debugging info
10#define DEBUG_printf(args...) (void)0
11#endif
12
Paul Sokolovskyef181022014-01-03 03:06:25 +020013#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +010014static int total_bytes_allocated = 0;
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020015static int current_bytes_allocated = 0;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020016static int peak_bytes_allocated = 0;
17
18#define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; }
Paul Sokolovskyef181022014-01-03 03:06:25 +020019#endif
Damien429d7192013-10-04 19:53:11 +010020
Damien429d7192013-10-04 19:53:11 +010021void *m_malloc(int num_bytes) {
22 if (num_bytes == 0) {
23 return NULL;
24 }
25 void *ptr = malloc(num_bytes);
26 if (ptr == NULL) {
27 printf("could not allocate memory, allocating %d bytes\n", num_bytes);
28 return NULL;
29 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020030#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +010031 total_bytes_allocated += num_bytes;
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020032 current_bytes_allocated += num_bytes;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020033 UPDATE_PEAK();
Paul Sokolovskyef181022014-01-03 03:06:25 +020034#endif
Damien George2d15c122014-01-29 20:33:20 +000035 DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
Damien429d7192013-10-04 19:53:11 +010036 return ptr;
37}
38
39void *m_malloc0(int num_bytes) {
40 if (num_bytes == 0) {
41 return NULL;
42 }
43 void *ptr = calloc(1, num_bytes);
44 if (ptr == NULL) {
45 printf("could not allocate memory, allocating %d bytes\n", num_bytes);
46 return NULL;
47 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020048#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +010049 total_bytes_allocated += num_bytes;
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020050 current_bytes_allocated += num_bytes;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020051 UPDATE_PEAK();
Paul Sokolovskyef181022014-01-03 03:06:25 +020052#endif
Damien George2d15c122014-01-29 20:33:20 +000053 DEBUG_printf("malloc0 %d : %p\n", num_bytes, ptr);
Damien429d7192013-10-04 19:53:11 +010054 return ptr;
55}
56
Damien732407f2013-12-29 19:33:23 +000057void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
58 if (new_num_bytes == 0) {
Damien429d7192013-10-04 19:53:11 +010059 free(ptr);
60 return NULL;
61 }
Paul Sokolovskycdd2c622014-01-30 03:58:17 +020062 void *new_ptr = realloc(ptr, new_num_bytes);
63 if (new_ptr == NULL) {
Damien732407f2013-12-29 19:33:23 +000064 printf("could not allocate memory, reallocating %d bytes\n", new_num_bytes);
Damien429d7192013-10-04 19:53:11 +010065 return NULL;
66 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020067#if MICROPY_MEM_STATS
Paul Sokolovsky43f1c802014-01-01 23:04:25 +020068 // At first thought, "Total bytes allocated" should only grow,
69 // after all, it's *total*. But consider for example 2K block
70 // shrunk to 1K and then grown to 2K again. It's still 2K
71 // allocated total. If we process only positive increments,
72 // we'll count 3K.
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020073 int diff = new_num_bytes - old_num_bytes;
74 total_bytes_allocated += diff;
75 current_bytes_allocated += diff;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020076 UPDATE_PEAK();
Paul Sokolovskyef181022014-01-03 03:06:25 +020077#endif
Paul Sokolovskycdd2c622014-01-30 03:58:17 +020078 DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
79 return new_ptr;
Damien429d7192013-10-04 19:53:11 +010080}
81
Damien732407f2013-12-29 19:33:23 +000082void m_free(void *ptr, int num_bytes) {
83 if (ptr != NULL) {
84 free(ptr);
85 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020086#if MICROPY_MEM_STATS
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020087 current_bytes_allocated -= num_bytes;
Paul Sokolovskyef181022014-01-03 03:06:25 +020088#endif
Damien George2d15c122014-01-29 20:33:20 +000089 DEBUG_printf("free %p, %d\n", ptr, num_bytes);
Damien732407f2013-12-29 19:33:23 +000090}
91
Damien8b3a7c22013-10-23 20:20:17 +010092int m_get_total_bytes_allocated(void) {
Paul Sokolovskyef181022014-01-03 03:06:25 +020093#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +010094 return total_bytes_allocated;
Paul Sokolovskyef181022014-01-03 03:06:25 +020095#else
96 return -1;
97#endif
Damien429d7192013-10-04 19:53:11 +010098}
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020099
100int m_get_current_bytes_allocated(void) {
Paul Sokolovskyef181022014-01-03 03:06:25 +0200101#if MICROPY_MEM_STATS
Paul Sokolovsky02de0c52014-01-01 23:15:47 +0200102 return current_bytes_allocated;
Paul Sokolovskyef181022014-01-03 03:06:25 +0200103#else
104 return -1;
105#endif
Paul Sokolovsky02de0c52014-01-01 23:15:47 +0200106}
Paul Sokolovsky780f5552014-01-01 23:42:21 +0200107
108int m_get_peak_bytes_allocated(void) {
Paul Sokolovskyef181022014-01-03 03:06:25 +0200109#if MICROPY_MEM_STATS
Paul Sokolovsky780f5552014-01-01 23:42:21 +0200110 return peak_bytes_allocated;
Paul Sokolovskyef181022014-01-03 03:06:25 +0200111#else
112 return -1;
113#endif
Paul Sokolovsky780f5552014-01-01 23:42:21 +0200114}