Move alloc.cc to maxbase

Only renames the functions. Macro names are left as is to keep the diff
small.
This commit is contained in:
Esa Korhonen
2018-11-30 15:21:07 +02:00
parent 0c7e737eb7
commit 760f2ff34c
8 changed files with 75 additions and 72 deletions

View File

@ -1,4 +1,5 @@
add_library(maxbase STATIC
alloc.cc
atomic.cc
eventcount.cc
format.cc

View File

@ -0,0 +1,205 @@
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2022-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxbase/alloc.h>
#include <stdlib.h>
#include <string.h>
#include <maxbase/log.h>
/**
* @brief Allocates memory; behaves exactly like `malloc`.
*
* Usually `mxs_malloc` is not used, but the macro `MXS_MALLOC` instead.
*
* @note The returned pointer can be passed to `mxs_realloc`, `mxs_realloc_a`
* and `mxs_free`.
*
* @param size The amount of memory to allocate.
* @param caller The name of the function calling this function.
* @return A pointer to the allocated memory.
*/
void* mxb_malloc(size_t size /*, const char *caller*/)
{
void* ptr = malloc(size);
if (!ptr)
{
// MXS_OOM_MESSAGE(caller);
MXB_OOM();
}
return ptr;
}
/**
* @brief Allocates memory; behaves exactly like `calloc`.
*
* Usually `mxs_calloc` is not used, but the macro `MXS_CALLOC` instead.
*
* @note The returned pointer can be passed to `mxs_realloc`, `mxs_realloc_a`
* and `mxs_free`.
*
* @param nmemb The number of elements.
* @param size The size of each element.
* @param caller The name of the function calling this function.
* @return A pointer to the allocated memory.
*/
void* mxb_calloc(size_t nmemb, size_t size /*, const char *caller*/)
{
void* ptr = calloc(nmemb, size);
if (!ptr)
{
// MXS_OOM_MESSAGE(caller);
MXB_OOM();
}
return ptr;
}
/**
* @brief Re-allocates memory; behaves exactly like `realloc`.
*
* Usually `mxs_realloc` is not used, but the macro `MXS_REALLOC` instead.
*
* @note The returned pointer can be passed to `mxs_realloc`, `mxs_realloc_a`
* and `mxs_free`.
*
* @param ptr Pointer to memory earlier allocated by `mxs_malloc`,
* `mxs_calloc`, `mxs_realloc`, `mxs_strdup`, `mxs_strndup`
* or or their `_a` equivalents.
* @param size What size the memory block should be changed to.
* @param caller The name of the function calling this function.
* @return A pointer to the allocated memory.
*/
void* mxb_realloc(void* ptr, size_t size /*, const char *caller*/)
{
ptr = realloc(ptr, size);
if (!ptr)
{
// MXS_OOM_MESSAGE(caller);
MXB_OOM();
}
return ptr;
}
/**
* @brief Duplicates a string; behaves exactly like `strdup`.
*
* Usually `mxs_strdup` is not used, but the macro `MXS_STRDUP` instead.
*
* @note The returned pointer can be passed to `mxs_realloc` and `mxs_free`.
*
* @param s1 The string to be duplicated.
* @param caller The name of the function calling this function.
* @return A copy of the string.
*/
char* mxb_strdup(const char* s1 /*, const char *caller*/)
{
char* s2 = strdup(s1);
if (!s2)
{
// MXS_OOM_MESSAGE(caller);
MXB_OOM();
}
return s2;
}
/**
* @brief Duplicates a string; behaves exactly like `strndup`.
*
* Usually `mxs_strndup` is not used, but the macro `MXS_STRNDUP` instead.
*
* @note The returned pointer can be passed to `mxs_realloc` and `mxs_free`.
*
* @param s1 The string to be duplicated.
* @param n At most n bytes should be copied.
* @param caller The name of the function calling this function.
* @return A copy of the string.
*/
char* mxb_strndup(const char* s1, size_t n /*, const char *caller*/)
{
char* s2 = strndup(s1, n);
if (!s2)
{
// MXS_OOM_MESSAGE(caller);
MXB_OOM();
}
return s2;
}
/**
* @brief Frees memory.
*
* Usually `mxs_free` is not used, but the macro `MXS_FREE` instead.
*
* @note The memory must have earlier been allocated with `mxs_malloc`,
* `mxs_calloc`, `mxs_realloc`, `mxs_strdup`, `mxs_strndup`, or
* their `_a` equivalents.
*
* @param ptr Pointer to the memory to be freed.
* @param caller The name of the function calling this function.
*/
void mxb_free(void* ptr /*, const char *caller*/)
{
free(ptr);
}
/**
* @brief Duplicates a string.
*
* Behaves exactly like `strdup`, except that `mxs_strdup_a` _always_
* returns a non-NULL pointer. In case `mxs_strdup_a` cannot do that,
* it _as_ the process.
*
* Usually `mxs_strdup_a` is not used, but the macro `MXS_STRDUP_A`
* instead.
*
* @note The returned pointer can be passed to `mxs_realloc` and `mxs_free`.
*
* @param s1 The string to be duplicated.
* @param caller The name of the function calling this function.
* @return A copy of the string.
*/
char* mxb_strdup_a(const char* s1 /*, const char *caller*/)
{
char* s2 = mxb_strdup(s1 /*, caller*/);
if (!s2)
{
abort();
}
return s2;
}
/**
* @breif Duplicates a string.
*
* Behaves exactly like `strndup` except that `mxs_strndup_a` _always_
* returns a non-NULL pointer. In case `mxs_strndup_a` cannot do that, it
* _as_ the process.
*
* Usually `mxs_strndup_a` is not used, but the macro MXS_STRNDUP_A
* instead.
*
* @note The returned pointer can be passed to `mxs_realloc` and `mxs_free`.
*
* @param s1 The string to be duplicated.
* @param n At most n bytes should be copied.
* @param caller The name of the function calling this function.
* @return A copy of the string.
*/
char* mxb_strndup_a(const char* s1, size_t n /*, const char *caller*/)
{
char* s2 = mxb_strndup(s1, n /*, caller*/);
if (!s2)
{
abort();
}
return s2;
}