diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | libs/libc/alloc.c | 63 | ||||
-rw-r--r-- | libs/libc/inc/crypto.h | 2 | ||||
-rw-r--r-- | libs/libc/inc/mem.h | 70 | ||||
-rw-r--r-- | libs/libc/inc/str.h | 20 | ||||
-rw-r--r-- | libs/libc/print.c | 2 |
6 files changed, 83 insertions, 76 deletions
@@ -9,7 +9,7 @@ endef PREPROCESSOR_FLAGS = $(foreach flag, $(ALL_PREPROCESSOR_FLAGS), $(call PREPROCESSOR_FLAG_TEMPLATE,$(flag))) -CFLAGS_WARNINGS = -Wall -Wextra -Werror -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wlogical-op -Wunreachable-code -Wundef -Wold-style-definition -Wvla -pedantic-errors +CFLAGS_WARNINGS = -Wall -Wextra -Werror -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wlogical-op -Wunreachable-code -Wundef -Wold-style-definition -Wvla -Winline -pedantic-errors CFLAGS_DEFAULT = $(CFLAGS_WARNINGS) -std=c99 -m32 -nostdlib -nostdinc -fno-builtin -fno-profile-generate -fno-omit-frame-pointer -fno-common -fno-asynchronous-unwind-tables -mno-red-zone -mno-80387 -mno-mmx -mno-sse -mno-sse2 $(CONFIG_OPTIMIZATION) $(CONFIG_EXTRA_CFLAGS) $(PREPROCESSOR_FLAGS) CC = $(CONFIG_CACHE) $(PWD)/cross/opt/bin/i686-elf-gcc diff --git a/libs/libc/alloc.c b/libs/libc/alloc.c index 73fdf8b..bc48502 100644 --- a/libs/libc/alloc.c +++ b/libs/libc/alloc.c @@ -114,7 +114,7 @@ static struct liballoc_major *allocate_new_page(u32 size) return maj; } -static void *_malloc(u32 req_size) +void *_malloc(u32 req_size) { req_size = ALIGN_UP(req_size, 16); @@ -289,15 +289,10 @@ static void *_malloc(u32 req_size) liballoc_unlock(); panic("Malloc failed!\n"); - return NULL; } -static void _free(void *ptr) +void _free(void *ptr) { - if (ptr == NULL) { - return; - } - liballoc_lock(); struct liballoc_minor *min = (struct liballoc_minor *)((u32)ptr - MINOR_SIZE); @@ -338,7 +333,7 @@ static void _free(void *ptr) liballoc_unlock(); } -static void *_realloc(void *ptr, u32 size) +void *_realloc(void *ptr, u32 size) { size = ALIGN_UP(size, 16); @@ -356,7 +351,6 @@ static void *_realloc(void *ptr, u32 size) if (min->magic != LIBALLOC_MAGIC) { liballoc_unlock(); panic("Malloc failed!\n"); - return NULL; } if (min->size >= size) { @@ -374,6 +368,13 @@ static void *_realloc(void *ptr, u32 size) return new_ptr; } +void *_zalloc(u32 size) +{ + void *ret = _malloc(size); + memset(ret, 0, size); + return ret; +} + #ifdef KERNEL #define PREFIX "K" #define FUNC printf @@ -382,53 +383,34 @@ static void *_realloc(void *ptr, u32 size) #define FUNC log #endif +#define LIMIT(size) assert(size < (100 << 20)) // Don't brag with memory pls + void *realloc_debug(void *ptr, u32 size, const char *file, int line, const char *func, - const char *inp) + const char *inp0, const char *inp1) { - assert(size < (100 << 20)); // Don't brag with memory pls + LIMIT(size); void *ret = _realloc(ptr, size); -#if DEBUG_ALLOC - FUNC(PREFIX "REALLOC\t%s:%d: %s: 0x%x %dB (%s)\n", file, line, func, ret, size, inp); -#else - UNUSED(file); - UNUSED(line); - UNUSED(func); - UNUSED(inp); -#endif + FUNC(PREFIX "REALLOC\t%s:%d: %s: 0x%x %dB (%s; %s)\n", file, line, func, ret, size, inp0, + inp1); return ret; } void *zalloc_debug(u32 size, const char *file, int line, const char *func, const char *inp) { - assert(size < (100 << 20)); // Don't brag with memory pls - void *ret = _malloc(size); - memset(ret, 0, size); + LIMIT(size); + void *ret = _zalloc(size); -#if DEBUG_ALLOC FUNC(PREFIX "ZALLOC\t%s:%d: %s: 0x%x %dB (%s)\n", file, line, func, ret, size, inp); -#else - UNUSED(file); - UNUSED(line); - UNUSED(func); - UNUSED(inp); -#endif return ret; } void *malloc_debug(u32 size, const char *file, int line, const char *func, const char *inp) { - assert(size < (100 << 20)); // Don't brag with memory pls + LIMIT(size); void *ret = _malloc(size); -#if DEBUG_ALLOC FUNC(PREFIX "MALLOC\t%s:%d: %s: 0x%x %dB (%s)\n", file, line, func, ret, size, inp); -#else - UNUSED(file); - UNUSED(line); - UNUSED(func); - UNUSED(inp); -#endif return ret; } @@ -436,12 +418,5 @@ void free_debug(void *ptr, const char *file, int line, const char *func, const c { _free(ptr); -#if DEBUG_ALLOC FUNC(PREFIX "FREE\t%s:%d: %s: 0x%x (%s)\n", file, line, func, ptr, inp); -#else - UNUSED(file); - UNUSED(line); - UNUSED(func); - UNUSED(inp); -#endif } diff --git a/libs/libc/inc/crypto.h b/libs/libc/inc/crypto.h index a67bb97..b672c50 100644 --- a/libs/libc/inc/crypto.h +++ b/libs/libc/inc/crypto.h @@ -9,7 +9,7 @@ void md5(const void *initial_msg, u32 initial_len, u8 digest[16]) NONNULL; u32 crc32(u32 crc, const void *buf, u32 size) NONNULL; #ifdef KERNEL -INLINE u32 crc32_user(u32 crc, const void *buf, u32 size) NONNULL; +u32 crc32_user(u32 crc, const void *buf, u32 size) NONNULL; #endif #endif diff --git a/libs/libc/inc/mem.h b/libs/libc/inc/mem.h index a25feaf..64f74f5 100644 --- a/libs/libc/inc/mem.h +++ b/libs/libc/inc/mem.h @@ -5,34 +5,66 @@ #include <def.h> -// malloc +/** + * malloc + */ + +ATTR((malloc)) ATTR((alloc_size(1))) RET_NONNULL void *_malloc(u32 size); + ATTR((malloc)) ATTR((alloc_size(1))) -INLINE RET_NONNULL void *malloc_debug(u32 size, const char *file, int line, const char *func, - const char *inp) NONNULL; +RET_NONNULL void *malloc_debug(u32 size, const char *file, int line, const char *func, + const char *inp) NONNULL; + +/** + * realloc + */ + +ATTR((malloc)) ATTR((alloc_size(2))) void *_realloc(void *ptr, u32 size); -// realloc ATTR((malloc)) ATTR((alloc_size(2))) -INLINE RET_NONNULL void *realloc_debug(void *ptr, u32 size, const char *file, int line, - const char *func, const char *inp); +void *realloc_debug(void *ptr, u32 size, const char *file, int line, const char *func, + const char *inp0, const char *inp1); + +/** + * zalloc + */ + +ATTR((malloc)) ATTR((alloc_size(1))) RET_NONNULL void *_zalloc(u32 size); -// zalloc ATTR((malloc)) ATTR((alloc_size(1))) -RET_NONNULL -INLINE void *zalloc_debug(u32 size, const char *file, int line, const char *func, const char *inp); +RET_NONNULL void *zalloc_debug(u32 size, const char *file, int line, const char *func, + const char *inp) NONNULL; + +/** + * free + */ + +void _free(void *ptr) NONNULL; +void free_debug(void *ptr, const char *file, int line, const char *func, const char *inp) NONNULL; -// free -INLINE void free_debug(void *ptr, const char *file, int line, const char *func, - const char *inp) NONNULL; +/** + * Debug wrappers + */ -// Debug wrappers +#if DEBUG_ALLOC #define realloc(ptr, size) \ - realloc_debug((void *)ptr, (u32)(size), __FILE__, __LINE__, __func__, #size) + realloc_debug((void *)ptr, (u32)(size), __FILE__, __LINE__, __func__, #ptr, #size) #define zalloc(size) zalloc_debug((u32)(size), __FILE__, __LINE__, __func__, #size) #define malloc(size) malloc_debug((u32)(size), __FILE__, __LINE__, __func__, #size) #define free(ptr) free_debug((void *)(ptr), __FILE__, __LINE__, __func__, #ptr) +#else +#define realloc(ptr, size) _realloc((void *)ptr, (u32)(size)) +#define zalloc(size) _zalloc((u32)(size)) +#define malloc(size) _malloc((u32)(size)) +#define free(ptr) _free((void *)(ptr)) +#endif + +/** + * Standard memory functions + */ void *memcpy(void *dest, const void *src, u32 n) NONNULL; void *memset(void *dest, u32 val, u32 n) NONNULL; @@ -41,11 +73,11 @@ s32 memcmp(const void *s1, const void *s2, u32 n) NONNULL; u8 mememp(const u8 *buf, u32 n) NONNULL; #ifdef KERNEL -INLINE void *memcpy_user(void *dest, const void *src, u32 n) NONNULL; -INLINE void *memset_user(void *dest, u32 val, u32 n) NONNULL; -INLINE void *memchr_user(void *src, char c, u32 n) NONNULL; -INLINE s32 memcmp_user(const void *s1, const void *s2, u32 n) NONNULL; -INLINE u8 mememp_user(const u8 *buf, u32 n) NONNULL; +void *memcpy_user(void *dest, const void *src, u32 n) NONNULL; +void *memset_user(void *dest, u32 val, u32 n) NONNULL; +void *memchr_user(void *src, char c, u32 n) NONNULL; +s32 memcmp_user(const void *s1, const void *s2, u32 n) NONNULL; +u8 mememp_user(const u8 *buf, u32 n) NONNULL; #endif #endif diff --git a/libs/libc/inc/str.h b/libs/libc/inc/str.h index d4c197e..f024aaa 100644 --- a/libs/libc/inc/str.h +++ b/libs/libc/inc/str.h @@ -18,16 +18,16 @@ ATTR((malloc)) char *strdup(const char *s) NONNULL; #ifdef KERNEL -INLINE PURE u32 strlen_user(const char *s) NONNULL; -INLINE PURE u32 strnlen_user(const char *s, u32 max) NONNULL; -INLINE u32 strlcpy_user(char *dst, const char *src, u32 size) NONNULL; -INLINE PURE char *strchr_user(char *s, char c) NONNULL; -INLINE PURE char *strrchr_user(char *s, char c) NONNULL; -INLINE u32 strlcat_user(char *dst, const char *src, u32 size) NONNULL; -INLINE s32 strcmp_user(const char *s1, const char *s2) NONNULL; -INLINE s32 strncmp_user(const char *s1, const char *s2, u32 n) NONNULL; -INLINE char *strinv_user(char *s) NONNULL; -INLINE ATTR((malloc)) char *strdup_user(const char *s) NONNULL; +PURE u32 strlen_user(const char *s) NONNULL; +PURE u32 strnlen_user(const char *s, u32 max) NONNULL; +u32 strlcpy_user(char *dst, const char *src, u32 size) NONNULL; +PURE char *strchr_user(char *s, char c) NONNULL; +PURE char *strrchr_user(char *s, char c) NONNULL; +u32 strlcat_user(char *dst, const char *src, u32 size) NONNULL; +s32 strcmp_user(const char *s1, const char *s2) NONNULL; +s32 strncmp_user(const char *s1, const char *s2, u32 n) NONNULL; +char *strinv_user(char *s) NONNULL; +ATTR((malloc)) char *strdup_user(const char *s) NONNULL; #endif diff --git a/libs/libc/print.c b/libs/libc/print.c index 2d08fdb..c4455eb 100644 --- a/libs/libc/print.c +++ b/libs/libc/print.c @@ -248,7 +248,7 @@ void print_trace(u32 count) #endif -NORETURN void panic(const char *format, ...) +void panic(const char *format, ...) { char buf[1024] = { 0 }; va_list ap; |