2008-04-01 18:52:26 +02:00
|
|
|
/*
|
|
|
|
* ircd-ratbox: A slightly useful ircd.
|
|
|
|
* memory.h: A header for the memory functions.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
|
|
|
|
* Copyright (C) 1996-2002 Hybrid Development Team
|
|
|
|
* Copyright (C) 2002-2005 ircd-ratbox development team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
* USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _RB_MEMORY_H
|
|
|
|
#define _RB_MEMORY_H
|
2016-07-13 07:17:21 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2008-04-01 18:52:26 +02:00
|
|
|
|
2016-06-23 03:15:58 +02:00
|
|
|
#define RB_UNIQUE_PTR(deleter) __attribute__((cleanup(deleter)))
|
|
|
|
#define RB_AUTO_PTR RB_UNIQUE_PTR(rb_raii_free)
|
2008-04-01 18:52:26 +02:00
|
|
|
|
2016-06-01 21:50:09 +02:00
|
|
|
void rb_outofmemory(void) __attribute__((noreturn));
|
2008-04-01 18:52:26 +02:00
|
|
|
|
2016-06-24 01:18:05 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
__attribute__((malloc, returns_nonnull))
|
|
|
|
#else
|
|
|
|
__attribute__((malloc, alloc_size(1), returns_nonnull))
|
|
|
|
#endif
|
2008-04-01 18:52:26 +02:00
|
|
|
static inline void *
|
|
|
|
rb_malloc(size_t size)
|
|
|
|
{
|
|
|
|
void *ret = calloc(1, size);
|
2008-06-25 07:28:30 +02:00
|
|
|
if(rb_unlikely(ret == NULL))
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_outofmemory();
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2016-06-24 01:18:05 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
__attribute__((returns_nonnull))
|
|
|
|
#else
|
|
|
|
__attribute__((alloc_size(2), returns_nonnull))
|
|
|
|
#endif
|
2008-04-01 18:52:26 +02:00
|
|
|
static inline void *
|
|
|
|
rb_realloc(void *x, size_t y)
|
|
|
|
{
|
|
|
|
void *ret = realloc(x, y);
|
|
|
|
|
2008-06-25 07:28:30 +02:00
|
|
|
if(rb_unlikely(ret == NULL))
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_outofmemory();
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2016-06-24 01:18:05 +02:00
|
|
|
__attribute__((returns_nonnull))
|
2008-04-01 18:52:26 +02:00
|
|
|
static inline char *
|
|
|
|
rb_strndup(const char *x, size_t y)
|
|
|
|
{
|
2016-06-21 02:45:31 +02:00
|
|
|
char *ret = (char *)malloc(y);
|
2008-06-25 07:28:30 +02:00
|
|
|
if(rb_unlikely(ret == NULL))
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_outofmemory();
|
|
|
|
rb_strlcpy(ret, x, y);
|
2008-12-03 00:49:39 +01:00
|
|
|
return (ret);
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 01:18:05 +02:00
|
|
|
__attribute__((returns_nonnull))
|
2008-04-01 18:52:26 +02:00
|
|
|
static inline char *
|
|
|
|
rb_strdup(const char *x)
|
|
|
|
{
|
2016-06-21 02:45:31 +02:00
|
|
|
char *ret = (char *)malloc(strlen(x) + 1);
|
2008-06-25 07:28:30 +02:00
|
|
|
if(rb_unlikely(ret == NULL))
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_outofmemory();
|
|
|
|
strcpy(ret, x);
|
2008-12-03 00:49:39 +01:00
|
|
|
return (ret);
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 01:28:36 +02:00
|
|
|
// overrule libc's prototype and ignore the cast warning, allowing proper const
|
|
|
|
// propagation without casting everywhere in the real code.
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-qual"
|
2008-04-01 18:52:26 +02:00
|
|
|
static inline void
|
2016-06-24 01:28:36 +02:00
|
|
|
rb_free(const void *const ptr)
|
2008-04-01 18:52:26 +02:00
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
if(rb_likely(ptr != NULL))
|
2016-06-24 01:28:36 +02:00
|
|
|
free((void *)ptr);
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
2016-06-24 01:28:36 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2016-06-23 03:15:58 +02:00
|
|
|
|
|
|
|
static inline void
|
|
|
|
rb_raii_free(const void *const ptr)
|
|
|
|
{
|
|
|
|
if(!ptr)
|
|
|
|
return;
|
|
|
|
|
2016-06-24 01:28:36 +02:00
|
|
|
const void *const _ptr = *(const void *const *)ptr;
|
2016-06-23 03:15:58 +02:00
|
|
|
if(!_ptr)
|
|
|
|
return;
|
|
|
|
|
2016-06-24 01:28:36 +02:00
|
|
|
rb_free(_ptr);
|
2016-06-23 03:15:58 +02:00
|
|
|
}
|
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
#endif /* _RB_MEMORY_H */
|