From 2df7ca985d053a91429598f4c53d53d6306ac421 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Tue, 20 Jan 2015 11:55:17 +0100 Subject: [PATCH] Removed malloc wrapper --- Makefile.am | 4 +-- configure.ac | 8 ----- src/mman.c | 9 ------ src/wrap_malloc.c | 80 ----------------------------------------------- src/wrap_malloc.h | 33 ------------------- 5 files changed, 1 insertion(+), 133 deletions(-) delete mode 100644 src/wrap_malloc.c delete mode 100644 src/wrap_malloc.h diff --git a/Makefile.am b/Makefile.am index 489550a..dc2c9ff 100644 --- a/Makefile.am +++ b/Makefile.am @@ -27,8 +27,6 @@ subdirinclude_HEADERS = \ libcsptr_la_SOURCES = \ src/mman.h \ - src/mman.c \ - src/wrap_malloc.h \ - src/wrap_malloc.c + src/mman.c man3_MANS = doc/csptr.3 diff --git a/configure.ac b/configure.ac index 9410640..21f5968 100644 --- a/configure.ac +++ b/configure.ac @@ -27,14 +27,6 @@ AC_ARG_WITH([fixed-allocator], [AC_DEFINE([SMALLOC_FIXED_ALLOCATOR], [1], [Define if malloc should always be used.])], []) -AC_ARG_WITH([malloc-wrapper], - [AS_HELP_STRING([--with-malloc-wrapper], [Make smart pointers compatible with a pass to free, and make malloc return a smart pointer.])], - [AC_DEFINE([SMALLOC_WRAP_MALLOC], [1], [Define if malloc should be wrapped]) - FLAG_WRAP_MALLOC="-Wl,--no-as-needed -ldl" - AC_SUBST([FLAG_WRAP_MALLOC]) - ], - []) - AC_ARG_ENABLE([gcov], [AS_HELP_STRING([--enable-gcov], [Compile the project with converage enabled])], diff --git a/src/mman.c b/src/mman.c index 03b521f..f2a4b79 100644 --- a/src/mman.c +++ b/src/mman.c @@ -31,7 +31,6 @@ #include "config.h" #include "mman.h" #include "array.h" -#include "wrap_malloc.h" #undef smalloc s_allocator smalloc_allocator = {malloc, free}; @@ -63,11 +62,7 @@ __attribute__ ((malloc)) INLINE static void *alloc_entry(size_t head, size_t size, size_t metasize) { const size_t totalsize = head + size + metasize + sizeof (size_t); #ifdef SMALLOC_FIXED_ALLOCATOR -# ifdef SMALLOC_WRAP_MALLOC - return real_malloc(totalsize); -# else /* !SMALLOC_WRAP_MALLOC */ return malloc(totalsize); -# endif /* !SMALLOC_WRAP_MALLOC */ #else /* !SMALLOC_FIXED_ALLOCATOR */ return smalloc_allocator.alloc(totalsize); #endif /* !SMALLOC_FIXED_ALLOCATOR */ @@ -85,11 +80,7 @@ INLINE static void dealloc_entry(s_meta *meta, void *ptr) { } #ifdef SMALLOC_FIXED_ALLOCATOR -# ifdef SMALLOC_WRAP_MALLOC - real_free(meta); -# else /* !SMALLOC_WRAP_MALLOC */ free(meta); -# endif /* !SMALLOC_WRAP_MALLOC */ #else /* !SMALLOC_FIXED_ALLOCATOR */ smalloc_allocator.dealloc(meta); #endif /* !SMALLOC_FIXED_ALLOCATOR */ diff --git a/src/wrap_malloc.c b/src/wrap_malloc.c deleted file mode 100644 index ffcc34a..0000000 --- a/src/wrap_malloc.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright © 2015 Franklin "Snaipe" Mathieu - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#define _GNU_SOURCE -#include -#include -#include - -#include "config.h" -#include "mman.h" -#undef smalloc - -#ifdef SMALLOC_WRAP_MALLOC -void *(*real_malloc)(size_t) = NULL; -void *(*real_realloc)(void *, size_t) = NULL; -void (*real_free)(void *) = NULL; - -__attribute__ ((constructor)) -static void initialize_handles(void) { -# if defined(__APPLE__) - void *handle = dlopen("libc.dylib", RTLD_NOW); - real_malloc = (__typeof__(real_malloc)) dlsym(handle, "malloc"); - real_realloc = (__typeof__(real_realloc)) dlsym(handle, "realloc"); - real_free = (__typeof__(real_free)) dlsym(handle, "free"); -# elif defined(__unix__) - real_malloc = (__typeof__(real_malloc)) dlsym(RTLD_NEXT, "malloc"); - real_realloc = (__typeof__(real_realloc)) dlsym(RTLD_NEXT, "realloc"); - real_free = (__typeof__(real_free)) dlsym(RTLD_NEXT, "free"); -# else -# error malloc wrapping not supported on non-*nix systems. -# endif - if (real_malloc == NULL || real_free == NULL) { - puts("Failed to locate original malloc or free; aborting.\n"); - exit(-1); - } - - smalloc_allocator = (s_allocator) {real_malloc, real_free}; -} - -__attribute__ ((malloc)) -void *malloc(size_t size) { - return smalloc(size, 0, UNIQUE, 0); -} - -__attribute__ ((malloc)) -void *calloc(size_t size, size_t nmemb) { - void *ptr = smalloc(size, nmemb, UNIQUE, 0); - memset(ptr, 0, size * nmemb); - return ptr; -} - -void free(void *ptr) { - sfree(ptr); -} - -void *realloc(void *ptr, size_t size) { - return real_realloc(ptr != NULL ? get_meta(ptr) : NULL, size); -} -#endif /* !SMALLOC_WRAP_MALLOC */ diff --git a/src/wrap_malloc.h b/src/wrap_malloc.h deleted file mode 100644 index 617bde2..0000000 --- a/src/wrap_malloc.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright © 2015 Franklin "Snaipe" Mathieu - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef CSPTR_WRAP_MALLOC_H_ -# define CSPTR_WRAP_MALLOC_H_ - -# ifdef SMALLOC_WRAP_MALLOC -extern void *(*real_malloc)(size_t); -extern void (*real_free)(void *); -# endif /* !SMALLOC_WRAP_MALLOC */ - -#endif /* !CSPTR_WRAP_MALLOC_H_ */