old-trainOS/include/stdlib.h

161 lines
2.8 KiB
C
Raw Normal View History

2015-08-09 00:42:56 +00:00
#pragma once
#include <stddef.h>
#include <inttypes.h>
#include "varargs.h"
#include "config.h"
2015-08-09 00:42:56 +00:00
2015-08-13 12:56:50 +00:00
#if defined(__cplusplus)
extern "C" {
#endif
2015-08-09 00:42:56 +00:00
char *itoa(int value, char *str, int base);
int atoi(const char *str);
float atof(const char *str);
#if defined(USE_VERBOSE_MALLOC)
void *malloc_d(size_t,const char *,int);
#endif
#if defined(USE_VERBOSE_FREE)
void free_d(void *,const char*, int);
#endif
2015-08-09 00:42:56 +00:00
/**
* Allocates a block of memory
* @param size Minimum size of the memory block
* @return Pointer to the allocated memory area
*/
void *malloc(size_t size);
/**
* Frees a previously allocated block of memory.
* @param mem The block of memory.
*/
void free(void *mem);
2015-08-13 12:56:50 +00:00
void* realloc (void* ptr, size_t size);
void exit(int errorCode);
2015-08-09 00:42:56 +00:00
static inline void *memset(void *ptr, int value, size_t num)
{
uint8_t *it = (uint8_t*)ptr;
while((num--) > 0)
{
*(it++) = (uint8_t)(value & 0xFF);
}
return ptr;
}
static inline void *memcpy(void *destination, const void *source, size_t num)
{
uint8_t *to = (uint8_t*)destination;
uint8_t *from = (uint8_t*)source;
while((num--) > 0)
{
*(to++) = *(from++);
}
return destination;
}
void *memmove(void *destination, const void *source, size_t num);
2015-08-13 12:56:50 +00:00
static inline int memcmp(const void *s1, const void *s2, size_t n)
{
const uint8_t * p1 = (const uint8_t *)s1;
const uint8_t * p2 = (const uint8_t *)s2;
for ( ; n-- ; p1++, p2++) {
uint8_t u1 = *p1;
uint8_t u2 = *p2;
if ( u1 != u2) {
return (u1-u2);
}
}
return 0;
}
2015-08-09 00:42:56 +00:00
static inline char *strcpy(char *destination, const char *source)
{
while(*source)
{
*(destination++) = *(source++);
}
return destination;
}
static inline char *strcat(char *destination, const char *source)
{
char *it = destination;
while(*it++);
it--;
while(*source)
{
*it++ = *source++;
}
*it = 0;
return destination;
}
static inline size_t strlen(const char *str)
{
size_t size = 0;
while(*(str++) != 0) size++;
return size;
}
static inline int strcmp(const char *p1, const char *p2)
{
while(*p1) {
if(*p2 == 0) {
return 1;
}
if(*p2 > *p1) {
return -1;
}
if(*p1 > *p2) {
return 1;
}
p1++;
p2++;
}
if (*p2) {
return -1;
}
return 0;
}
2015-08-09 00:42:56 +00:00
static inline void *calloc(size_t size)
{
void *mem = malloc(size);
memset(mem, 0, size);
return mem;
}
2015-08-13 12:56:50 +00:00
static inline char * strdup(const char *str)
{
size_t len = strlen(str) + 1;
char * n = (char*)malloc(len);
memcpy(n, str, len);
return n;
}
int sprintf(char *target, const char *format, ...);
int vsprintf(char *target, const char *format, va_list vl);
2015-08-13 12:56:50 +00:00
#if defined(__cplusplus)
}
#endif
#if defined(USE_VERBOSE_MALLOC)
#define malloc(size) malloc_d((size), __FILE__, __LINE__)
#endif
#if defined(USE_VERBOSE_FREE)
#define free(ptr) free_d((ptr), __FILE__, __LINE__)
#endif