| 1 | /* |
| 2 | * Copyright (c) 2005 Hewlett-Packard Development Company, L.P. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | /* Almost lock-free malloc implementation based on stack implementation. */ |
| 24 | /* See README_malloc.txt file for detailed usage rules. */ |
| 25 | |
| 26 | #ifndef AO_MALLOC_H |
| 27 | #define AO_MALLOC_H |
| 28 | |
| 29 | #include "atomic_ops_stack.h" |
| 30 | |
| 31 | #include <stddef.h> /* for size_t */ |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | #ifdef AO_STACK_IS_LOCK_FREE |
| 38 | # define AO_MALLOC_IS_LOCK_FREE |
| 39 | #endif |
| 40 | |
| 41 | #ifndef AO_ATTR_MALLOC |
| 42 | # if AO_GNUC_PREREQ(3, 1) |
| 43 | # define AO_ATTR_MALLOC __attribute__((__malloc__)) |
| 44 | # elif defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(__EDG__) |
| 45 | # define AO_ATTR_MALLOC \ |
| 46 | __declspec(allocator) __declspec(noalias) __declspec(restrict) |
| 47 | # elif defined(_MSC_VER) && _MSC_VER >= 1400 |
| 48 | # define AO_ATTR_MALLOC __declspec(noalias) __declspec(restrict) |
| 49 | # else |
| 50 | # define AO_ATTR_MALLOC /* empty */ |
| 51 | # endif |
| 52 | #endif |
| 53 | |
| 54 | #ifndef AO_ATTR_ALLOC_SIZE |
| 55 | # ifdef __clang__ |
| 56 | # if __has_attribute(__alloc_size__) |
| 57 | # define AO_ATTR_ALLOC_SIZE(argnum) \ |
| 58 | __attribute__((__alloc_size__(argnum))) |
| 59 | # else |
| 60 | # define AO_ATTR_ALLOC_SIZE(argnum) /* empty */ |
| 61 | # endif |
| 62 | # elif AO_GNUC_PREREQ(4, 3) && !defined(__ICC) |
| 63 | # define AO_ATTR_ALLOC_SIZE(argnum) __attribute__((__alloc_size__(argnum))) |
| 64 | # else |
| 65 | # define AO_ATTR_ALLOC_SIZE(argnum) /* empty */ |
| 66 | # endif |
| 67 | #endif |
| 68 | |
| 69 | AO_API void AO_free(void *); |
| 70 | |
| 71 | AO_API AO_ATTR_MALLOC AO_ATTR_ALLOC_SIZE(1) |
| 72 | void * AO_malloc(size_t); |
| 73 | |
| 74 | /* Allow use of mmap to grow the heap. No-op on some platforms. */ |
| 75 | AO_API void AO_malloc_enable_mmap(void); |
| 76 | |
| 77 | #ifdef __cplusplus |
| 78 | } /* extern "C" */ |
| 79 | #endif |
| 80 | |
| 81 | #endif /* !AO_MALLOC_H */ |
| 82 | |