Empty malloc() definition in /bare_arm/main.c

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
p-i-
Posts: 20
Joined: Sun Sep 14, 2014 2:24 pm

Empty malloc() definition in /bare_arm/main.c

Post by p-i- » Wed Sep 17, 2014 11:47 am

I'm never sure whether to post in General or Dev, as my project is very close to the line. For the moment, I'm trying to fiddle µPy to get it building on JUCE rather than trying to push source code modifications to trunk, so I guess it makes more sense to post here. And it looks as though the core devs participate in both forums.

I am looking through /bare_arm, with the aim of constructing a minimal 'hello world' build.

I can't understand why main.c contains the following:

Code: Select all

void *malloc(size_t n) {return NULL;}
void *calloc(size_t nmemb, size_t size) {return NULL;}
void *realloc(void *ptr, size_t size) {return NULL;}
void free(void *p) {}
int printf(const char *m, ...) {return 0;}
void *memcpy(void *dest, const void *src, size_t n) {return NULL;}
int memcmp(const void *s1, const void *s2, size_t n) {return 0;}
void *memmove(void *dest, const void *src, size_t n) {return NULL;}
void *memset(void *s, int c, size_t n) {return NULL;}
int strcmp(const char *s1, const char* s2) {return 0;}
int strncmp(const char *s1, const char* s2, size_t n) {return 0;}
size_t strlen(const char *s) {return 0;}
char *strcat(char *dest, const char *src) {return NULL;}
char *strchr(const char *dest, int c) {return NULL;}
#include <stdarg.h>
int vprintf(const char *format, va_list ap) {return 0;}
int vsnprintf(char *str,  size_t  size,  const  char  *format, va_list ap) {return 0;}

#undef putchar
int putchar(int c) {return 0;}
int puts(const char *s) {return 0;}

void _start(void) {main(0, NULL);}
What is the purpose of this?

dhylands suggested this may be to test whether it builds.

But why not just #include <memory.h> and friends, and have something that builds and runs?

π

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Empty malloc() definition in /bare_arm/main.c

Post by stijn » Fri Sep 19, 2014 7:39 am

dhylands suggested this may be to test whether it builds.
That certainly looks like it: a minimal build as proof of concept for compilation.

But why not just #include <memory.h> and friends, and have something that builds and runs?
Once you do that you depend on system libraries so you don't have a minimal build anymore. (just guessing here, but I can't think of any other reason)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Empty malloc() definition in /bare_arm/main.c

Post by dhylands » Fri Sep 19, 2014 2:28 pm

[quote="p-i-"]But why not just #include <memory.h> and friends, and have something that builds and runs?
/quote]

#include <memory.h> just gives you the prototype. You still need to link against a runtime library or provide an implementation. The stmhal version of micropython doesn't link against any runtime library (for example).

Post Reply