xref: /freebsd/contrib/ntp/sntp/libevent/mm-internal.h (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
32b15cb3dSCy Schubert  *
42b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
52b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
62b15cb3dSCy Schubert  * are met:
72b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
82b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
92b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
102b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
112b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
122b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
132b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
142b15cb3dSCy Schubert  *
152b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252b15cb3dSCy Schubert  */
262b15cb3dSCy Schubert #ifndef MM_INTERNAL_H_INCLUDED_
272b15cb3dSCy Schubert #define MM_INTERNAL_H_INCLUDED_
282b15cb3dSCy Schubert 
292b15cb3dSCy Schubert #include <sys/types.h>
302b15cb3dSCy Schubert 
312b15cb3dSCy Schubert #ifdef __cplusplus
322b15cb3dSCy Schubert extern "C" {
332b15cb3dSCy Schubert #endif
342b15cb3dSCy Schubert 
352b15cb3dSCy Schubert #ifndef EVENT__DISABLE_MM_REPLACEMENT
362b15cb3dSCy Schubert /* Internal use only: Memory allocation functions. We give them nice short
372b15cb3dSCy Schubert  * mm_names for our own use, but make sure that the symbols have longer names
382b15cb3dSCy Schubert  * so they don't conflict with other libraries (like, say, libmm). */
392b15cb3dSCy Schubert 
402b15cb3dSCy Schubert /** Allocate uninitialized memory.
412b15cb3dSCy Schubert  *
422b15cb3dSCy Schubert  * @return On success, return a pointer to sz newly allocated bytes.
432b15cb3dSCy Schubert  *     On failure, set errno to ENOMEM and return NULL.
442b15cb3dSCy Schubert  *     If the argument sz is 0, simply return NULL.
452b15cb3dSCy Schubert  */
46*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
472b15cb3dSCy Schubert void *event_mm_malloc_(size_t sz);
482b15cb3dSCy Schubert 
492b15cb3dSCy Schubert /** Allocate memory initialized to zero.
502b15cb3dSCy Schubert  *
512b15cb3dSCy Schubert  * @return On success, return a pointer to (count * size) newly allocated
522b15cb3dSCy Schubert  *     bytes, initialized to zero.
532b15cb3dSCy Schubert  *     On failure, or if the product would result in an integer overflow,
542b15cb3dSCy Schubert  *     set errno to ENOMEM and return NULL.
552b15cb3dSCy Schubert  *     If either arguments are 0, simply return NULL.
562b15cb3dSCy Schubert  */
57*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
582b15cb3dSCy Schubert void *event_mm_calloc_(size_t count, size_t size);
592b15cb3dSCy Schubert 
602b15cb3dSCy Schubert /** Duplicate a string.
612b15cb3dSCy Schubert  *
622b15cb3dSCy Schubert  * @return On success, return a pointer to a newly allocated duplicate
632b15cb3dSCy Schubert  *     of a string.
642b15cb3dSCy Schubert  *     Set errno to ENOMEM and return NULL if a memory allocation error
652b15cb3dSCy Schubert  *     occurs (or would occur) in the process.
662b15cb3dSCy Schubert  *     If the argument str is NULL, set errno to EINVAL and return NULL.
672b15cb3dSCy Schubert  */
68*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
692b15cb3dSCy Schubert char *event_mm_strdup_(const char *str);
702b15cb3dSCy Schubert 
71*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
722b15cb3dSCy Schubert void *event_mm_realloc_(void *p, size_t sz);
73*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
742b15cb3dSCy Schubert void event_mm_free_(void *p);
752b15cb3dSCy Schubert #define mm_malloc(sz) event_mm_malloc_(sz)
762b15cb3dSCy Schubert #define mm_calloc(count, size) event_mm_calloc_((count), (size))
772b15cb3dSCy Schubert #define mm_strdup(s) event_mm_strdup_(s)
782b15cb3dSCy Schubert #define mm_realloc(p, sz) event_mm_realloc_((p), (sz))
792b15cb3dSCy Schubert #define mm_free(p) event_mm_free_(p)
802b15cb3dSCy Schubert #else
812b15cb3dSCy Schubert #define mm_malloc(sz) malloc(sz)
822b15cb3dSCy Schubert #define mm_calloc(n, sz) calloc((n), (sz))
832b15cb3dSCy Schubert #define mm_strdup(s) strdup(s)
842b15cb3dSCy Schubert #define mm_realloc(p, sz) realloc((p), (sz))
852b15cb3dSCy Schubert #define mm_free(p) free(p)
862b15cb3dSCy Schubert #endif
872b15cb3dSCy Schubert 
882b15cb3dSCy Schubert #ifdef __cplusplus
892b15cb3dSCy Schubert }
902b15cb3dSCy Schubert #endif
912b15cb3dSCy Schubert 
922b15cb3dSCy Schubert #endif
93