Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | #include "misc.h" |
| 5 | |
| 6 | static int total_bytes_allocated = 0; |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame^] | 7 | static int current_bytes_allocated = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 8 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 9 | void *m_malloc(int num_bytes) { |
| 10 | if (num_bytes == 0) { |
| 11 | return NULL; |
| 12 | } |
| 13 | void *ptr = malloc(num_bytes); |
| 14 | if (ptr == NULL) { |
| 15 | printf("could not allocate memory, allocating %d bytes\n", num_bytes); |
| 16 | return NULL; |
| 17 | } |
| 18 | total_bytes_allocated += num_bytes; |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame^] | 19 | current_bytes_allocated += num_bytes; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 20 | return ptr; |
| 21 | } |
| 22 | |
| 23 | void *m_malloc0(int num_bytes) { |
| 24 | if (num_bytes == 0) { |
| 25 | return NULL; |
| 26 | } |
| 27 | void *ptr = calloc(1, num_bytes); |
| 28 | if (ptr == NULL) { |
| 29 | printf("could not allocate memory, allocating %d bytes\n", num_bytes); |
| 30 | return NULL; |
| 31 | } |
| 32 | total_bytes_allocated += num_bytes; |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame^] | 33 | current_bytes_allocated += num_bytes; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 34 | return ptr; |
| 35 | } |
| 36 | |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 37 | void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) { |
| 38 | if (new_num_bytes == 0) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 39 | free(ptr); |
| 40 | return NULL; |
| 41 | } |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 42 | ptr = realloc(ptr, new_num_bytes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 43 | if (ptr == NULL) { |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 44 | printf("could not allocate memory, reallocating %d bytes\n", new_num_bytes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 45 | return NULL; |
| 46 | } |
Paul Sokolovsky | 43f1c80 | 2014-01-01 23:04:25 +0200 | [diff] [blame] | 47 | // At first thought, "Total bytes allocated" should only grow, |
| 48 | // after all, it's *total*. But consider for example 2K block |
| 49 | // shrunk to 1K and then grown to 2K again. It's still 2K |
| 50 | // allocated total. If we process only positive increments, |
| 51 | // we'll count 3K. |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame^] | 52 | int diff = new_num_bytes - old_num_bytes; |
| 53 | total_bytes_allocated += diff; |
| 54 | current_bytes_allocated += diff; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 55 | return ptr; |
| 56 | } |
| 57 | |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 58 | void m_free(void *ptr, int num_bytes) { |
| 59 | if (ptr != NULL) { |
| 60 | free(ptr); |
| 61 | } |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame^] | 62 | current_bytes_allocated -= num_bytes; |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 65 | int m_get_total_bytes_allocated(void) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 66 | return total_bytes_allocated; |
| 67 | } |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame^] | 68 | |
| 69 | int m_get_current_bytes_allocated(void) { |
| 70 | return current_bytes_allocated; |
| 71 | } |