Skip to content
Snippets Groups Projects
Commit 58ff93bc authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

Get rid of calloc().

If there's malloc and memset, then there's no need for calloc, especially if
we need to implement it ourselves.
parent 2e24ee8d
Branches
Tags
No related merge requests found
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "misc.h" #include "misc.h"
#include "mpconfig.h" #include "mpconfig.h"
...@@ -37,20 +38,10 @@ void *m_malloc(int num_bytes) { ...@@ -37,20 +38,10 @@ void *m_malloc(int num_bytes) {
} }
void *m_malloc0(int num_bytes) { void *m_malloc0(int num_bytes) {
if (num_bytes == 0) { void *ptr = m_malloc(num_bytes);
return NULL; if (ptr != NULL) {
} memset(ptr, 0, num_bytes);
void *ptr = calloc(1, num_bytes);
if (ptr == NULL) {
printf("could not allocate memory, allocating %d bytes\n", num_bytes);
return NULL;
} }
#if MICROPY_MEM_STATS
total_bytes_allocated += num_bytes;
current_bytes_allocated += num_bytes;
UPDATE_PEAK();
#endif
DEBUG_printf("malloc0 %d : %p\n", num_bytes, ptr);
return ptr; return ptr;
} }
......
...@@ -29,14 +29,6 @@ void *realloc(void *ptr, size_t n) { ...@@ -29,14 +29,6 @@ void *realloc(void *ptr, size_t n) {
#endif #endif
void *calloc(size_t sz, size_t n) {
char *ptr = malloc(sz * n);
for (int i = 0; i < sz * n; i++) {
ptr[i] = 0;
}
return ptr;
}
void *malloc(size_t n) { void *malloc(size_t n) {
return gc_alloc(n); return gc_alloc(n);
} }
......
...@@ -4,7 +4,6 @@ void __assert_func(void); ...@@ -4,7 +4,6 @@ void __assert_func(void);
void *malloc(size_t n); void *malloc(size_t n);
void free(void *ptr); void free(void *ptr);
void *calloc(size_t sz, size_t n);
void *realloc(void *ptr, size_t n); void *realloc(void *ptr, size_t n);
void *memcpy(void *dest, const void *src, size_t n); void *memcpy(void *dest, const void *src, size_t n);
......
...@@ -4,7 +4,6 @@ void __assert_func(void); ...@@ -4,7 +4,6 @@ void __assert_func(void);
void *malloc(size_t n); void *malloc(size_t n);
void free(void *ptr); void free(void *ptr);
void *calloc(size_t sz, size_t n);
void *realloc(void *ptr, size_t n); void *realloc(void *ptr, size_t n);
void *memcpy(void *dest, const void *src, size_t n); void *memcpy(void *dest, const void *src, size_t n);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment