Annotate AO_malloc with 'alloc_size' and 'malloc' attributes
[platform/upstream/libatomic_ops.git] / src / atomic_ops_malloc.h
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 doc/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 AO_STACK_IS_LOCK_FREE
34 # define AO_MALLOC_IS_LOCK_FREE
35 #endif
36
37 #ifndef AO_ATTR_MALLOC
38 # if AO_GNUC_PREREQ(3, 1)
39 #   define AO_ATTR_MALLOC __attribute__((__malloc__))
40 # elif defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(__EDG__)
41 #   define AO_ATTR_MALLOC \
42                 __declspec(allocator) __declspec(noalias) __declspec(restrict)
43 # elif defined(_MSC_VER) && _MSC_VER >= 1400
44 #   define AO_ATTR_MALLOC __declspec(noalias) __declspec(restrict)
45 # else
46 #   define AO_ATTR_MALLOC /* empty */
47 # endif
48 #endif
49
50 #ifndef AO_ATTR_ALLOC_SIZE
51 # ifdef __clang__
52 #   if __has_attribute(__alloc_size__)
53 #     define AO_ATTR_ALLOC_SIZE(argnum) \
54                 __attribute__((__alloc_size__(argnum)))
55 #   else
56 #     define AO_ATTR_ALLOC_SIZE(argnum) /* empty */
57 #   endif
58 # elif AO_GNUC_PREREQ(4, 3) && !defined(__ICC)
59 #   define AO_ATTR_ALLOC_SIZE(argnum) __attribute__((__alloc_size__(argnum)))
60 # else
61 #   define AO_ATTR_ALLOC_SIZE(argnum) /* empty */
62 # endif
63 #endif
64
65 void AO_free(void *);
66
67 AO_ATTR_MALLOC AO_ATTR_ALLOC_SIZE(1)
68 void * AO_malloc(size_t);
69
70 /* Allow use of mmap to grow the heap.  No-op on some platforms.        */
71 void AO_malloc_enable_mmap(void);
72
73 #endif /* !AO_MALLOC_H */