xref: /libevent-2.1.12/mm-internal.h (revision 9806b126)
1b85b710cSNick Mathewson /*
2e49e2891SNick Mathewson  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
3b85b710cSNick Mathewson  *
4b85b710cSNick Mathewson  * Redistribution and use in source and binary forms, with or without
5b85b710cSNick Mathewson  * modification, are permitted provided that the following conditions
6b85b710cSNick Mathewson  * are met:
7b85b710cSNick Mathewson  * 1. Redistributions of source code must retain the above copyright
8b85b710cSNick Mathewson  *    notice, this list of conditions and the following disclaimer.
9b85b710cSNick Mathewson  * 2. Redistributions in binary form must reproduce the above copyright
10b85b710cSNick Mathewson  *    notice, this list of conditions and the following disclaimer in the
11b85b710cSNick Mathewson  *    documentation and/or other materials provided with the distribution.
12b85b710cSNick Mathewson  * 3. The name of the author may not be used to endorse or promote products
13b85b710cSNick Mathewson  *    derived from this software without specific prior written permission.
14b85b710cSNick Mathewson  *
15b85b710cSNick Mathewson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16b85b710cSNick Mathewson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17b85b710cSNick Mathewson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18b85b710cSNick Mathewson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19b85b710cSNick Mathewson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20b85b710cSNick Mathewson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21b85b710cSNick Mathewson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22b85b710cSNick Mathewson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23b85b710cSNick Mathewson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24b85b710cSNick Mathewson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25b85b710cSNick Mathewson  */
263f8c7cd0SNick Mathewson #ifndef MM_INTERNAL_H_INCLUDED_
273f8c7cd0SNick Mathewson #define MM_INTERNAL_H_INCLUDED_
28555e300aSNick Mathewson 
29555e300aSNick Mathewson #include <sys/types.h>
30555e300aSNick Mathewson 
31555e300aSNick Mathewson #ifdef __cplusplus
32555e300aSNick Mathewson extern "C" {
33555e300aSNick Mathewson #endif
34555e300aSNick Mathewson 
3568120d9bSNick Mathewson #ifndef EVENT__DISABLE_MM_REPLACEMENT
3699e50e90SNick Mathewson /* Internal use only: Memory allocation functions. We give them nice short
3799e50e90SNick Mathewson  * mm_names for our own use, but make sure that the symbols have longer names
3899e50e90SNick Mathewson  * so they don't conflict with other libraries (like, say, libmm). */
39c8953d1bSMansour Moufid 
40c8953d1bSMansour Moufid /** Allocate uninitialized memory.
41c8953d1bSMansour Moufid  *
42c8953d1bSMansour Moufid  * @return On success, return a pointer to sz newly allocated bytes.
43c8953d1bSMansour Moufid  *     On failure, set errno to ENOMEM and return NULL.
44c8953d1bSMansour Moufid  *     If the argument sz is 0, simply return NULL.
45c8953d1bSMansour Moufid  */
46*9806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
4799e50e90SNick Mathewson void *event_mm_malloc_(size_t sz);
48c8953d1bSMansour Moufid 
49c8953d1bSMansour Moufid /** Allocate memory initialized to zero.
50c8953d1bSMansour Moufid  *
51c8953d1bSMansour Moufid  * @return On success, return a pointer to (count * size) newly allocated
52c8953d1bSMansour Moufid  *     bytes, initialized to zero.
53c8953d1bSMansour Moufid  *     On failure, or if the product would result in an integer overflow,
54c8953d1bSMansour Moufid  *     set errno to ENOMEM and return NULL.
55c8953d1bSMansour Moufid  *     If either arguments are 0, simply return NULL.
56c8953d1bSMansour Moufid  */
57*9806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
5899e50e90SNick Mathewson void *event_mm_calloc_(size_t count, size_t size);
59c8953d1bSMansour Moufid 
60c8953d1bSMansour Moufid /** Duplicate a string.
61c8953d1bSMansour Moufid  *
62c8953d1bSMansour Moufid  * @return On success, return a pointer to a newly allocated duplicate
63c8953d1bSMansour Moufid  *     of a string.
64c8953d1bSMansour Moufid  *     Set errno to ENOMEM and return NULL if a memory allocation error
65c8953d1bSMansour Moufid  *     occurs (or would occur) in the process.
66c8953d1bSMansour Moufid  *     If the argument str is NULL, set errno to EINVAL and return NULL.
67c8953d1bSMansour Moufid  */
68*9806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
69c8953d1bSMansour Moufid char *event_mm_strdup_(const char *str);
70c8953d1bSMansour Moufid 
71*9806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
7299e50e90SNick Mathewson void *event_mm_realloc_(void *p, size_t sz);
73*9806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
7499e50e90SNick Mathewson void event_mm_free_(void *p);
7599e50e90SNick Mathewson #define mm_malloc(sz) event_mm_malloc_(sz)
7699e50e90SNick Mathewson #define mm_calloc(count, size) event_mm_calloc_((count), (size))
7799e50e90SNick Mathewson #define mm_strdup(s) event_mm_strdup_(s)
7899e50e90SNick Mathewson #define mm_realloc(p, sz) event_mm_realloc_((p), (sz))
7999e50e90SNick Mathewson #define mm_free(p) event_mm_free_(p)
807fa8451dSNick Mathewson #else
817fa8451dSNick Mathewson #define mm_malloc(sz) malloc(sz)
827fa8451dSNick Mathewson #define mm_calloc(n, sz) calloc((n), (sz))
837fa8451dSNick Mathewson #define mm_strdup(s) strdup(s)
847fa8451dSNick Mathewson #define mm_realloc(p, sz) realloc((p), (sz))
857fa8451dSNick Mathewson #define mm_free(p) free(p)
867fa8451dSNick Mathewson #endif
87555e300aSNick Mathewson 
88555e300aSNick Mathewson #ifdef __cplusplus
89555e300aSNick Mathewson }
90555e300aSNick Mathewson #endif
91555e300aSNick Mathewson 
92555e300aSNick Mathewson #endif
93