18153Sphk/*
28153Sphk * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
38153Sphk *
48153Sphk * Redistribution and use in source and binary forms, with or without
58153Sphk * modification, are permitted provided that the following conditions
68153Sphk * are met:
78153Sphk * 1. Redistributions of source code must retain the above copyright
88153Sphk *    notice, this list of conditions and the following disclaimer.
914792Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
108153Sphk *    notice, this list of conditions and the following disclaimer in the
118153Sphk *    documentation and/or other materials provided with the distribution.
128153Sphk * 3. The name of the author may not be used to endorse or promote products
138153Sphk *    derived from this software without specific prior written permission.
148153Sphk *
158153Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
168153Sphk * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
178153Sphk * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188153Sphk * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
198153Sphk * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
208158Sphk * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
218153Sphk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
228153Sphk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
238153Sphk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
248153Sphk * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258153Sphk */
268153Sphk#ifndef MM_INTERNAL_H_INCLUDED_
278881Srgrimes#define MM_INTERNAL_H_INCLUDED_
288153Sphk
2914792Sjoerg#include <sys/types.h>
308241Sjkh
318241Sjkh#ifdef __cplusplus
328241Sjkhextern "C" {
338241Sjkh#endif
348241Sjkh
358241Sjkh#ifndef EVENT__DISABLE_MM_REPLACEMENT
368241Sjkh/* Internal use only: Memory allocation functions. We give them nice short
378241Sjkh * mm_names for our own use, but make sure that the symbols have longer names
388241Sjkh * so they don't conflict with other libraries (like, say, libmm). */
398241Sjkh
408153Sphk/** Allocate uninitialized memory.
4114792Sjoerg *
428153Sphk * @return On success, return a pointer to sz newly allocated bytes.
438153Sphk *     On failure, set errno to ENOMEM and return NULL.
448153Sphk *     If the argument sz is 0, simply return NULL.
458153Sphk */
468153Sphkvoid *event_mm_malloc_(size_t sz);
4714792Sjoerg
488153Sphk/** Allocate memory initialized to zero.
498153Sphk *
508153Sphk * @return On success, return a pointer to (count * size) newly allocated
518160Sphk *     bytes, initialized to zero.
528153Sphk *     On failure, or if the product would result in an integer overflow,
538153Sphk *     set errno to ENOMEM and return NULL.
548178Sphk *     If either arguments are 0, simply return NULL.
558178Sphk */
568346Sphkvoid *event_mm_calloc_(size_t count, size_t size);
578153Sphk
588153Sphk/** Duplicate a string.
598153Sphk *
608153Sphk * @return On success, return a pointer to a newly allocated duplicate
618178Sphk *     of a string.
628178Sphk *     Set errno to ENOMEM and return NULL if a memory allocation error
638178Sphk *     occurs (or would occur) in the process.
648178Sphk *     If the argument str is NULL, set errno to EINVAL and return NULL.
658153Sphk */
668153Sphkchar *event_mm_strdup_(const char *str);
678153Sphk
688153Sphkvoid *event_mm_realloc_(void *p, size_t sz);
698153Sphkvoid event_mm_free_(void *p);
708178Sphk#define mm_malloc(sz) event_mm_malloc_(sz)
718178Sphk#define mm_calloc(count, size) event_mm_calloc_((count), (size))
728178Sphk#define mm_strdup(s) event_mm_strdup_(s)
738153Sphk#define mm_realloc(p, sz) event_mm_realloc_((p), (sz))
748153Sphk#define mm_free(p) event_mm_free_(p)
758160Sphk#else
768153Sphk#define mm_malloc(sz) malloc(sz)
778153Sphk#define mm_calloc(n, sz) calloc((n), (sz))
788153Sphk#define mm_strdup(s) strdup(s)
798153Sphk#define mm_realloc(p, sz) realloc((p), (sz))
808233Sphk#define mm_free(p) free(p)
818183Sphk#endif
828183Sphk
838183Sphk#ifdef __cplusplus
848183Sphk}
858183Sphk#endif
868227Sjkh
878183Sphk#endif
888178Sphk