blob: 1765eb6743bf7e8a34bdd57d495c1d62ae981a92 [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"
6#include "defaultconfig.h"
Damien429d7192013-10-04 19:53:11 +01007
Paul Sokolovskyef181022014-01-03 03:06:25 +02008#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +01009static int total_bytes_allocated = 0;
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020010static int current_bytes_allocated = 0;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020011static int peak_bytes_allocated = 0;
12
13#define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; }
Paul Sokolovskyef181022014-01-03 03:06:25 +020014#endif
Damien429d7192013-10-04 19:53:11 +010015
Damien429d7192013-10-04 19:53:11 +010016void *m_malloc(int num_bytes) {
17 if (num_bytes == 0) {
18 return NULL;
19 }
20 void *ptr = malloc(num_bytes);
21 if (ptr == NULL) {
22 printf("could not allocate memory, allocating %d bytes\n", num_bytes);
23 return NULL;
24 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020025#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +010026 total_bytes_allocated += num_bytes;
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020027 current_bytes_allocated += num_bytes;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020028 UPDATE_PEAK();
Paul Sokolovskyef181022014-01-03 03:06:25 +020029#endif
Damien429d7192013-10-04 19:53:11 +010030 return ptr;
31}
32
33void *m_malloc0(int num_bytes) {
34 if (num_bytes == 0) {
35 return NULL;
36 }
37 void *ptr = calloc(1, num_bytes);
38 if (ptr == NULL) {
39 printf("could not allocate memory, allocating %d bytes\n", num_bytes);
40 return NULL;
41 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020042#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +010043 total_bytes_allocated += num_bytes;
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020044 current_bytes_allocated += num_bytes;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020045 UPDATE_PEAK();
Paul Sokolovskyef181022014-01-03 03:06:25 +020046#endif
Damien429d7192013-10-04 19:53:11 +010047 return ptr;
48}
49
Damien732407f2013-12-29 19:33:23 +000050void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
51 if (new_num_bytes == 0) {
Damien429d7192013-10-04 19:53:11 +010052 free(ptr);
53 return NULL;
54 }
Damien732407f2013-12-29 19:33:23 +000055 ptr = realloc(ptr, new_num_bytes);
Damien429d7192013-10-04 19:53:11 +010056 if (ptr == NULL) {
Damien732407f2013-12-29 19:33:23 +000057 printf("could not allocate memory, reallocating %d bytes\n", new_num_bytes);
Damien429d7192013-10-04 19:53:11 +010058 return NULL;
59 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020060#if MICROPY_MEM_STATS
Paul Sokolovsky43f1c802014-01-01 23:04:25 +020061 // At first thought, "Total bytes allocated" should only grow,
62 // after all, it's *total*. But consider for example 2K block
63 // shrunk to 1K and then grown to 2K again. It's still 2K
64 // allocated total. If we process only positive increments,
65 // we'll count 3K.
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020066 int diff = new_num_bytes - old_num_bytes;
67 total_bytes_allocated += diff;
68 current_bytes_allocated += diff;
Paul Sokolovsky780f5552014-01-01 23:42:21 +020069 UPDATE_PEAK();
Paul Sokolovskyef181022014-01-03 03:06:25 +020070#endif
Damien429d7192013-10-04 19:53:11 +010071 return ptr;
72}
73
Damien732407f2013-12-29 19:33:23 +000074void m_free(void *ptr, int num_bytes) {
75 if (ptr != NULL) {
76 free(ptr);
77 }
Paul Sokolovskyef181022014-01-03 03:06:25 +020078#if MICROPY_MEM_STATS
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020079 current_bytes_allocated -= num_bytes;
Paul Sokolovskyef181022014-01-03 03:06:25 +020080#endif
Damien732407f2013-12-29 19:33:23 +000081}
82
Damien8b3a7c22013-10-23 20:20:17 +010083int m_get_total_bytes_allocated(void) {
Paul Sokolovskyef181022014-01-03 03:06:25 +020084#if MICROPY_MEM_STATS
Damien429d7192013-10-04 19:53:11 +010085 return total_bytes_allocated;
Paul Sokolovskyef181022014-01-03 03:06:25 +020086#else
87 return -1;
88#endif
Damien429d7192013-10-04 19:53:11 +010089}
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020090
91int m_get_current_bytes_allocated(void) {
Paul Sokolovskyef181022014-01-03 03:06:25 +020092#if MICROPY_MEM_STATS
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020093 return current_bytes_allocated;
Paul Sokolovskyef181022014-01-03 03:06:25 +020094#else
95 return -1;
96#endif
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020097}
Paul Sokolovsky780f5552014-01-01 23:42:21 +020098
99int m_get_peak_bytes_allocated(void) {
Paul Sokolovskyef181022014-01-03 03:06:25 +0200100#if MICROPY_MEM_STATS
Paul Sokolovsky780f5552014-01-01 23:42:21 +0200101 return peak_bytes_allocated;
Paul Sokolovskyef181022014-01-03 03:06:25 +0200102#else
103 return -1;
104#endif
Paul Sokolovsky780f5552014-01-01 23:42:21 +0200105}