Path: blob/master/libs/c++abi/src/fallback_malloc.cpp
12346 views
//===------------------------ fallback_malloc.cpp -------------------------===//1//2// The LLVM Compiler Infrastructure3//4// This file is dual licensed under the MIT and the University of Illinois Open5// Source Licenses. See LICENSE.TXT for details.6//7//===----------------------------------------------------------------------===//89// Define _LIBCPP_BUILDING_LIBRARY to ensure _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION10// is only defined when libc aligned allocation is not available.11#define _LIBCPP_BUILDING_LIBRARY12#include "fallback_malloc.h"1314#include <__threading_support>1516#include <cstdlib> // for malloc, calloc, free17#include <cstring> // for memset1819// A small, simple heap manager based (loosely) on20// the startup heap manager from FreeBSD, optimized for space.21//22// Manages a fixed-size memory pool, supports malloc and free only.23// No support for realloc.24//25// Allocates chunks in multiples of four bytes, with a four byte header26// for each chunk. The overhead of each chunk is kept low by keeping pointers27// as two byte offsets within the heap, rather than (4 or 8 byte) pointers.2829namespace {3031// When POSIX threads are not available, make the mutex operations a nop32#ifndef _LIBCXXABI_HAS_NO_THREADS33_LIBCPP_SAFE_STATIC34static std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;35#else36static void* heap_mutex = 0;37#endif3839class mutexor {40public:41#ifndef _LIBCXXABI_HAS_NO_THREADS42mutexor(std::__libcpp_mutex_t* m) : mtx_(m) {43std::__libcpp_mutex_lock(mtx_);44}45~mutexor() { std::__libcpp_mutex_unlock(mtx_); }46#else47mutexor(void*) {}48~mutexor() {}49#endif50private:51mutexor(const mutexor& rhs);52mutexor& operator=(const mutexor& rhs);53#ifndef _LIBCXXABI_HAS_NO_THREADS54std::__libcpp_mutex_t* mtx_;55#endif56};5758static const size_t HEAP_SIZE = 512;59char heap[HEAP_SIZE] __attribute__((aligned));6061typedef unsigned short heap_offset;62typedef unsigned short heap_size;6364struct heap_node {65heap_offset next_node; // offset into heap66heap_size len; // size in units of "sizeof(heap_node)"67};6869static const heap_node* list_end =70(heap_node*)(&heap[HEAP_SIZE]); // one past the end of the heap71static heap_node* freelist = NULL;7273heap_node* node_from_offset(const heap_offset offset) {74return (heap_node*)(heap + (offset * sizeof(heap_node)));75}7677heap_offset offset_from_node(const heap_node* ptr) {78return static_cast<heap_offset>(79static_cast<size_t>(reinterpret_cast<const char*>(ptr) - heap) /80sizeof(heap_node));81}8283void init_heap() {84freelist = (heap_node*)heap;85freelist->next_node = offset_from_node(list_end);86freelist->len = HEAP_SIZE / sizeof(heap_node);87}8889// How big a chunk we allocate90size_t alloc_size(size_t len) {91return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1;92}9394bool is_fallback_ptr(void* ptr) {95return ptr >= heap && ptr < (heap + HEAP_SIZE);96}9798void* fallback_malloc(size_t len) {99heap_node *p, *prev;100const size_t nelems = alloc_size(len);101mutexor mtx(&heap_mutex);102103if (NULL == freelist)104init_heap();105106// Walk the free list, looking for a "big enough" chunk107for (p = freelist, prev = 0; p && p != list_end;108prev = p, p = node_from_offset(p->next_node)) {109110if (p->len > nelems) { // chunk is larger, shorten, and return the tail111heap_node* q;112113p->len = static_cast<heap_size>(p->len - nelems);114q = p + p->len;115q->next_node = 0;116q->len = static_cast<heap_size>(nelems);117return (void*)(q + 1);118}119120if (p->len == nelems) { // exact size match121if (prev == 0)122freelist = node_from_offset(p->next_node);123else124prev->next_node = p->next_node;125p->next_node = 0;126return (void*)(p + 1);127}128}129return NULL; // couldn't find a spot big enough130}131132// Return the start of the next block133heap_node* after(struct heap_node* p) { return p + p->len; }134135void fallback_free(void* ptr) {136struct heap_node* cp = ((struct heap_node*)ptr) - 1; // retrieve the chunk137struct heap_node *p, *prev;138139mutexor mtx(&heap_mutex);140141#ifdef DEBUG_FALLBACK_MALLOC142std::cout << "Freeing item at " << offset_from_node(cp) << " of size "143<< cp->len << std::endl;144#endif145146for (p = freelist, prev = 0; p && p != list_end;147prev = p, p = node_from_offset(p->next_node)) {148#ifdef DEBUG_FALLBACK_MALLOC149std::cout << " p, cp, after (p), after(cp) " << offset_from_node(p) << ' '150<< offset_from_node(cp) << ' ' << offset_from_node(after(p))151<< ' ' << offset_from_node(after(cp)) << std::endl;152#endif153if (after(p) == cp) {154#ifdef DEBUG_FALLBACK_MALLOC155std::cout << " Appending onto chunk at " << offset_from_node(p)156<< std::endl;157#endif158p->len = static_cast<heap_size>(159p->len + cp->len); // make the free heap_node larger160return;161} else if (after(cp) == p) { // there's a free heap_node right after162#ifdef DEBUG_FALLBACK_MALLOC163std::cout << " Appending free chunk at " << offset_from_node(p)164<< std::endl;165#endif166cp->len = static_cast<heap_size>(cp->len + p->len);167if (prev == 0) {168freelist = cp;169cp->next_node = p->next_node;170} else171prev->next_node = offset_from_node(cp);172return;173}174}175// Nothing to merge with, add it to the start of the free list176#ifdef DEBUG_FALLBACK_MALLOC177std::cout << " Making new free list entry " << offset_from_node(cp)178<< std::endl;179#endif180cp->next_node = offset_from_node(freelist);181freelist = cp;182}183184#ifdef INSTRUMENT_FALLBACK_MALLOC185size_t print_free_list() {186struct heap_node *p, *prev;187heap_size total_free = 0;188if (NULL == freelist)189init_heap();190191for (p = freelist, prev = 0; p && p != list_end;192prev = p, p = node_from_offset(p->next_node)) {193std::cout << (prev == 0 ? "" : " ") << "Offset: " << offset_from_node(p)194<< "\tsize: " << p->len << " Next: " << p->next_node << std::endl;195total_free += p->len;196}197std::cout << "Total Free space: " << total_free << std::endl;198return total_free;199}200#endif201} // end unnamed namespace202203namespace __cxxabiv1 {204205struct __attribute__((aligned)) __aligned_type {};206207void* __aligned_malloc_with_fallback(size_t size) {208#if defined(_WIN32)209if (void* dest = _aligned_malloc(size, alignof(__aligned_type)))210return dest;211#elif defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)212if (void* dest = std::malloc(size))213return dest;214#else215if (size == 0)216size = 1;217void* dest;218if (::posix_memalign(&dest, __alignof(__aligned_type), size) == 0)219return dest;220#endif221return fallback_malloc(size);222}223224void* __calloc_with_fallback(size_t count, size_t size) {225void* ptr = std::calloc(count, size);226if (NULL != ptr)227return ptr;228// if calloc fails, fall back to emergency stash229ptr = fallback_malloc(size * count);230if (NULL != ptr)231std::memset(ptr, 0, size * count);232return ptr;233}234235void __aligned_free_with_fallback(void* ptr) {236if (is_fallback_ptr(ptr))237fallback_free(ptr);238else {239#if defined(_WIN32)240::_aligned_free(ptr);241#else242std::free(ptr);243#endif244}245}246247void __free_with_fallback(void* ptr) {248if (is_fallback_ptr(ptr))249fallback_free(ptr);250else251std::free(ptr);252}253254} // namespace __cxxabiv1255256257