Commit 27ca0db7 authored by Max Kellermann's avatar Max Kellermann

util/Alloc: new library replacing GLib's g_malloc()

parent 49f34fbf
...@@ -251,6 +251,7 @@ libutil_a_SOURCES = \ ...@@ -251,6 +251,7 @@ libutil_a_SOURCES = \
src/util/Macros.hxx \ src/util/Macros.hxx \
src/util/Cast.hxx \ src/util/Cast.hxx \
src/util/Clamp.hxx \ src/util/Clamp.hxx \
src/util/Alloc.cxx src/util/Alloc.hxx \
src/util/VarSize.hxx \ src/util/VarSize.hxx \
src/util/Error.cxx src/util/Error.hxx \ src/util/Error.cxx src/util/Error.hxx \
src/util/Domain.hxx \ src/util/Domain.hxx \
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
struct DecoderBuffer { struct DecoderBuffer {
Decoder *decoder; Decoder *decoder;
......
...@@ -174,7 +174,7 @@ Directory::LookupDirectory(const char *uri) ...@@ -174,7 +174,7 @@ Directory::LookupDirectory(const char *uri)
if (isRootDirectory(uri)) if (isRootDirectory(uri))
return this; return this;
char *duplicated = g_strdup(uri), *name = duplicated; char *duplicated = xstrdup(uri), *name = duplicated;
Directory *d = this; Directory *d = this;
while (1) { while (1) {
...@@ -194,7 +194,7 @@ Directory::LookupDirectory(const char *uri) ...@@ -194,7 +194,7 @@ Directory::LookupDirectory(const char *uri)
name = slash + 1; name = slash + 1;
} }
g_free(duplicated); free(duplicated);
return d; return d;
} }
...@@ -244,7 +244,7 @@ Directory::LookupSong(const char *uri) ...@@ -244,7 +244,7 @@ Directory::LookupSong(const char *uri)
assert(holding_db_lock()); assert(holding_db_lock());
assert(uri != nullptr); assert(uri != nullptr);
duplicated = g_strdup(uri); duplicated = xstrdup(uri);
base = strrchr(duplicated, '/'); base = strrchr(duplicated, '/');
Directory *d = this; Directory *d = this;
...@@ -252,7 +252,7 @@ Directory::LookupSong(const char *uri) ...@@ -252,7 +252,7 @@ Directory::LookupSong(const char *uri)
*base++ = 0; *base++ = 0;
d = d->LookupDirectory(duplicated); d = d->LookupDirectory(duplicated);
if (d == nullptr) { if (d == nullptr) {
g_free(duplicated); free(duplicated);
return nullptr; return nullptr;
} }
} else } else
...@@ -261,7 +261,7 @@ Directory::LookupSong(const char *uri) ...@@ -261,7 +261,7 @@ Directory::LookupSong(const char *uri)
Song *song = d->FindSong(base); Song *song = d->FindSong(base);
assert(song == nullptr || song->parent == d); assert(song == nullptr || song->parent == d);
g_free(duplicated); free(duplicated);
return song; return song;
} }
......
...@@ -19,19 +19,19 @@ ...@@ -19,19 +19,19 @@
#include "config.h" #include "config.h"
#include "Page.hxx" #include "Page.hxx"
#include "util/Alloc.hxx"
#include <glib.h>
#include <new> #include <new>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
Page * Page *
Page::Create(size_t size) Page::Create(size_t size)
{ {
void *p = g_malloc(sizeof(Page) + size - void *p = xalloc(sizeof(Page) + size -
sizeof(Page::data)); sizeof(Page::data));
return ::new(p) Page(size); return ::new(p) Page(size);
} }
...@@ -63,7 +63,7 @@ Page::Unref() ...@@ -63,7 +63,7 @@ Page::Unref()
if (unused) { if (unused) {
this->Page::~Page(); this->Page::~Page();
g_free(this); free(this);
} }
return unused; return unused;
......
...@@ -28,12 +28,11 @@ ...@@ -28,12 +28,11 @@
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "fs/Traits.hxx" #include "fs/Traits.hxx"
#include "fs/FileSystem.hxx" #include "fs/FileSystem.hxx"
#include "util/Alloc.hxx"
#include "util/UriUtil.hxx" #include "util/UriUtil.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <glib.h>
#include <string.h> #include <string.h>
void void
...@@ -127,7 +126,7 @@ playlist_load_spl(struct playlist &playlist, PlayerControl &pc, ...@@ -127,7 +126,7 @@ playlist_load_spl(struct playlist &playlist, PlayerControl &pc,
if ((playlist.AppendURI(pc, uri_utf8.c_str())) != PlaylistResult::SUCCESS) { if ((playlist.AppendURI(pc, uri_utf8.c_str())) != PlaylistResult::SUCCESS) {
/* for windows compatibility, convert slashes */ /* for windows compatibility, convert slashes */
char *temp2 = g_strdup(uri_utf8.c_str()); char *temp2 = xstrdup(uri_utf8.c_str());
char *p = temp2; char *p = temp2;
while (*p) { while (*p) {
if (*p == '\\') if (*p == '\\')
...@@ -139,7 +138,7 @@ playlist_load_spl(struct playlist &playlist, PlayerControl &pc, ...@@ -139,7 +138,7 @@ playlist_load_spl(struct playlist &playlist, PlayerControl &pc,
FormatError(playlist_domain, FormatError(playlist_domain,
"can't add file \"%s\"", temp2); "can't add file \"%s\"", temp2);
g_free(temp2); free(temp2);
} }
} }
......
...@@ -21,11 +21,11 @@ ...@@ -21,11 +21,11 @@
#include "Song.hxx" #include "Song.hxx"
#include "Directory.hxx" #include "Directory.hxx"
#include "tag/Tag.hxx" #include "tag/Tag.hxx"
#include "util/Alloc.hxx"
#include <glib.h>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
Directory detached_root; Directory detached_root;
...@@ -39,7 +39,7 @@ song_alloc(const char *uri, Directory *parent) ...@@ -39,7 +39,7 @@ song_alloc(const char *uri, Directory *parent)
assert(uri_length); assert(uri_length);
Song *song = (Song *) Song *song = (Song *)
g_malloc(sizeof(*song) - sizeof(song->uri) + uri_length + 1); xalloc(sizeof(*song) - sizeof(song->uri) + uri_length + 1);
song->tag = nullptr; song->tag = nullptr;
memcpy(song->uri, uri, uri_length + 1); memcpy(song->uri, uri, uri_length + 1);
...@@ -72,7 +72,7 @@ Song::ReplaceURI(const char *new_uri) ...@@ -72,7 +72,7 @@ Song::ReplaceURI(const char *new_uri)
new_song->mtime = mtime; new_song->mtime = mtime;
new_song->start_ms = start_ms; new_song->start_ms = start_ms;
new_song->end_ms = end_ms; new_song->end_ms = end_ms;
g_free(this); free(this);
return new_song; return new_song;
} }
...@@ -106,7 +106,7 @@ void ...@@ -106,7 +106,7 @@ void
Song::Free() Song::Free()
{ {
delete tag; delete tag;
g_free(this); free(this);
} }
void void
......
...@@ -38,14 +38,14 @@ ...@@ -38,14 +38,14 @@
#include "fs/Traits.hxx" #include "fs/Traits.hxx"
#include "fs/FileSystem.hxx" #include "fs/FileSystem.hxx"
#include "fs/DirectoryReader.hxx" #include "fs/DirectoryReader.hxx"
#include "util/Alloc.hxx"
#include "util/UriUtil.hxx" #include "util/UriUtil.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <glib.h>
#include <assert.h> #include <assert.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <errno.h> #include <errno.h>
bool walk_discard; bool walk_discard;
...@@ -426,7 +426,7 @@ static Directory * ...@@ -426,7 +426,7 @@ static Directory *
directory_make_uri_parent_checked(const char *uri) directory_make_uri_parent_checked(const char *uri)
{ {
Directory *directory = db_get_root(); Directory *directory = db_get_root();
char *duplicated = g_strdup(uri); char *duplicated = xstrdup(uri);
char *name_utf8 = duplicated, *slash; char *name_utf8 = duplicated, *slash;
while ((slash = strchr(name_utf8, '/')) != nullptr) { while ((slash = strchr(name_utf8, '/')) != nullptr) {
...@@ -443,7 +443,7 @@ directory_make_uri_parent_checked(const char *uri) ...@@ -443,7 +443,7 @@ directory_make_uri_parent_checked(const char *uri)
name_utf8 = slash + 1; name_utf8 = slash + 1;
} }
g_free(duplicated); free(duplicated);
return directory; return directory;
} }
......
...@@ -19,13 +19,12 @@ ...@@ -19,13 +19,12 @@
#include "config.h" #include "config.h"
#include "CueParser.hxx" #include "CueParser.hxx"
#include "util/Alloc.hxx"
#include "util/StringUtil.hxx" #include "util/StringUtil.hxx"
#include "util/CharUtil.hxx" #include "util/CharUtil.hxx"
#include "Song.hxx" #include "Song.hxx"
#include "tag/Tag.hxx" #include "tag/Tag.hxx"
#include <glib.h>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -290,9 +289,9 @@ CueParser::Feed(const char *line) ...@@ -290,9 +289,9 @@ CueParser::Feed(const char *line)
assert(!end); assert(!end);
assert(line != nullptr); assert(line != nullptr);
char *allocated = g_strdup(line); char *allocated = xstrdup(line);
Feed2(allocated); Feed2(allocated);
g_free(allocated); free(allocated);
} }
void void
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "DecoderAPI.hxx" #include "DecoderAPI.hxx"
#include "CheckAudioFormat.hxx" #include "CheckAudioFormat.hxx"
#include "tag/TagHandler.hxx" #include "tag/TagHandler.hxx"
#include "util/Alloc.hxx"
#include "util/FormatString.hxx" #include "util/FormatString.hxx"
#include "util/UriUtil.hxx" #include "util/UriUtil.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
...@@ -53,7 +54,7 @@ static char * ...@@ -53,7 +54,7 @@ static char *
get_container_name(const char *path_fs) get_container_name(const char *path_fs)
{ {
const char *subtune_suffix = uri_get_suffix(path_fs); const char *subtune_suffix = uri_get_suffix(path_fs);
char *path_container = g_strdup(path_fs); char *path_container = xstrdup(path_fs);
char pat[64]; char pat[64];
snprintf(pat, sizeof(pat), "%s%s", snprintf(pat, sizeof(pat), "%s%s",
...@@ -137,7 +138,7 @@ gme_file_decode(Decoder &decoder, const char *path_fs) ...@@ -137,7 +138,7 @@ gme_file_decode(Decoder &decoder, const char *path_fs)
Music_Emu *emu; Music_Emu *emu;
const char *gme_err = const char *gme_err =
gme_open_file(path_container, &emu, GME_SAMPLE_RATE); gme_open_file(path_container, &emu, GME_SAMPLE_RATE);
g_free(path_container); free(path_container);
if (gme_err != nullptr) { if (gme_err != nullptr) {
LogWarning(gme_domain, gme_err); LogWarning(gme_domain, gme_err);
return; return;
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "SidplayDecoderPlugin.hxx" #include "SidplayDecoderPlugin.hxx"
#include "../DecoderAPI.hxx" #include "../DecoderAPI.hxx"
#include "tag/TagHandler.hxx" #include "tag/TagHandler.hxx"
#include "util/Alloc.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "system/ByteOrder.hxx" #include "system/ByteOrder.hxx"
#include "Log.hxx" #include "Log.hxx"
...@@ -121,7 +122,7 @@ sidplay_finish() ...@@ -121,7 +122,7 @@ sidplay_finish()
static char * static char *
get_container_name(const char *path_fs) get_container_name(const char *path_fs)
{ {
char *path_container=g_strdup(path_fs); char *path_container = strdup(path_fs);
if(!g_pattern_match(path_with_subtune, if(!g_pattern_match(path_with_subtune,
strlen(path_container), path_container, nullptr)) strlen(path_container), path_container, nullptr))
...@@ -163,9 +164,9 @@ get_song_length(const char *path_fs) ...@@ -163,9 +164,9 @@ get_song_length(const char *path_fs)
if (songlength_database == nullptr) if (songlength_database == nullptr)
return -1; return -1;
gchar *sid_file=get_container_name(path_fs); char *sid_file = get_container_name(path_fs);
SidTuneMod tune(sid_file); SidTuneMod tune(sid_file);
g_free(sid_file); free(sid_file);
if(!tune) { if(!tune) {
LogWarning(sidplay_domain, LogWarning(sidplay_domain,
"failed to load file for calculating md5 sum"); "failed to load file for calculating md5 sum");
......
...@@ -31,12 +31,11 @@ ...@@ -31,12 +31,11 @@
#include "system/fd_util.h" #include "system/fd_util.h"
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "fs/FileSystem.hxx" #include "fs/FileSystem.hxx"
#include "util/Alloc.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <glib.h>
#include <string> #include <string>
#include <algorithm> #include <algorithm>
...@@ -79,7 +78,7 @@ public: ...@@ -79,7 +78,7 @@ public:
parent(_parent), serial(_serial), parent(_parent), serial(_serial),
path(AllocatedPath::Null()), path(AllocatedPath::Null()),
address_length(_address_length), address_length(_address_length),
address((sockaddr *)g_memdup(_address, _address_length)) address((sockaddr *)xmemdup(_address, _address_length))
{ {
assert(_address != nullptr); assert(_address != nullptr);
assert(_address_length > 0); assert(_address_length > 0);
...@@ -89,7 +88,7 @@ public: ...@@ -89,7 +88,7 @@ public:
OneServerSocket &operator=(const OneServerSocket &other) = delete; OneServerSocket &operator=(const OneServerSocket &other) = delete;
~OneServerSocket() { ~OneServerSocket() {
g_free(address); free(address);
if (IsDefined()) if (IsDefined())
Close(); Close();
......
...@@ -26,9 +26,10 @@ ...@@ -26,9 +26,10 @@
#include "ArchiveFile.hxx" #include "ArchiveFile.hxx"
#include "InputPlugin.hxx" #include "InputPlugin.hxx"
#include "fs/Traits.hxx" #include "fs/Traits.hxx"
#include "util/Alloc.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <glib.h> #include <stdlib.h>
/** /**
* select correct archive plugin to handle the input stream * select correct archive plugin to handle the input stream
...@@ -49,13 +50,13 @@ input_archive_open(const char *pathname, ...@@ -49,13 +50,13 @@ input_archive_open(const char *pathname,
if (!PathTraitsFS::IsAbsolute(pathname)) if (!PathTraitsFS::IsAbsolute(pathname))
return nullptr; return nullptr;
char *pname = g_strdup(pathname); char *pname = strdup(pathname);
// archive_lookup will modify pname when true is returned // archive_lookup will modify pname when true is returned
const char *archive, *filename, *suffix; const char *archive, *filename, *suffix;
if (!archive_lookup(pname, &archive, &filename, &suffix)) { if (!archive_lookup(pname, &archive, &filename, &suffix)) {
FormatDebug(archive_domain, FormatDebug(archive_domain,
"not an archive, lookup %s failed", pname); "not an archive, lookup %s failed", pname);
g_free(pname); free(pname);
return nullptr; return nullptr;
} }
...@@ -64,19 +65,19 @@ input_archive_open(const char *pathname, ...@@ -64,19 +65,19 @@ input_archive_open(const char *pathname,
if (!arplug) { if (!arplug) {
FormatWarning(archive_domain, FormatWarning(archive_domain,
"can't handle archive %s", archive); "can't handle archive %s", archive);
g_free(pname); free(pname);
return nullptr; return nullptr;
} }
auto file = archive_file_open(arplug, archive, error); auto file = archive_file_open(arplug, archive, error);
if (file == nullptr) { if (file == nullptr) {
g_free(pname); free(pname);
return nullptr; return nullptr;
} }
//setup fileops //setup fileops
is = file->OpenStream(filename, mutex, cond, error); is = file->OpenStream(filename, mutex, cond, error);
g_free(pname); free(pname);
file->Close(); file->Close();
return is; return is;
......
...@@ -72,7 +72,7 @@ osx_output_configure(OSXOutput *oo, const config_param &param) ...@@ -72,7 +72,7 @@ osx_output_configure(OSXOutput *oo, const config_param &param)
} }
else { else {
oo->component_subtype = kAudioUnitSubType_HALOutput; oo->component_subtype = kAudioUnitSubType_HALOutput;
/* XXX am I supposed to g_strdup() this? */ /* XXX am I supposed to strdup() this? */
oo->device_name = device; oo->device_name = device;
} }
} }
......
...@@ -24,10 +24,9 @@ ...@@ -24,10 +24,9 @@
#include "TagString.hxx" #include "TagString.hxx"
#include "Tag.hxx" #include "Tag.hxx"
#include <glib.h>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
TagBuilder::TagBuilder(const Tag &other) TagBuilder::TagBuilder(const Tag &other)
:time(other.time), has_playlist(other.has_playlist) :time(other.time), has_playlist(other.has_playlist)
...@@ -187,7 +186,7 @@ TagBuilder::AddItemInternal(TagType type, const char *value, size_t length) ...@@ -187,7 +186,7 @@ TagBuilder::AddItemInternal(TagType type, const char *value, size_t length)
auto i = tag_pool_get_item(type, value, length); auto i = tag_pool_get_item(type, value, length);
tag_pool_lock.unlock(); tag_pool_lock.unlock();
g_free(p); free(p);
items.push_back(i); items.push_back(i);
} }
......
...@@ -24,12 +24,15 @@ ...@@ -24,12 +24,15 @@
#include "ConfigGlobal.hxx" #include "ConfigGlobal.hxx"
#include "ConfigOption.hxx" #include "ConfigOption.hxx"
#include "system/FatalError.hxx" #include "system/FatalError.hxx"
#include "util/Alloc.hxx"
#include "util/ASCII.hxx" #include "util/ASCII.hxx"
#include <glib.h> #include <glib.h>
#include <algorithm> #include <algorithm>
#include <stdlib.h>
void void
TagLoadConfig() TagLoadConfig()
{ {
...@@ -44,7 +47,7 @@ TagLoadConfig() ...@@ -44,7 +47,7 @@ TagLoadConfig()
bool quit = false; bool quit = false;
char *temp, *c, *s; char *temp, *c, *s;
temp = c = s = g_strdup(value); temp = c = s = xstrdup(value);
do { do {
if (*s == ',' || *s == '\0') { if (*s == ',' || *s == '\0') {
if (*s == '\0') if (*s == '\0')
...@@ -68,5 +71,5 @@ TagLoadConfig() ...@@ -68,5 +71,5 @@ TagLoadConfig()
s++; s++;
} while (!quit); } while (!quit);
g_free(temp); free(temp);
} }
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
Mutex tag_pool_lock; Mutex tag_pool_lock;
......
...@@ -19,11 +19,13 @@ ...@@ -19,11 +19,13 @@
#include "config.h" #include "config.h"
#include "TagString.hxx" #include "TagString.hxx"
#include "util/Alloc.hxx"
#include <glib.h> #include <glib.h>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
/** /**
* Replace invalid sequences with the question mark. * Replace invalid sequences with the question mark.
...@@ -33,7 +35,7 @@ patch_utf8(const char *src, size_t length, const gchar *end) ...@@ -33,7 +35,7 @@ patch_utf8(const char *src, size_t length, const gchar *end)
{ {
/* duplicate the string, and replace invalid bytes in that /* duplicate the string, and replace invalid bytes in that
buffer */ buffer */
char *dest = g_strdup(src); char *dest = xstrdup(src);
do { do {
dest[end - src] = '?'; dest[end - src] = '?';
...@@ -58,9 +60,12 @@ fix_utf8(const char *str, size_t length) ...@@ -58,9 +60,12 @@ fix_utf8(const char *str, size_t length)
/* no, it's not - try to import it from ISO-Latin-1 */ /* no, it's not - try to import it from ISO-Latin-1 */
temp = g_convert(str, length, "utf-8", "iso-8859-1", temp = g_convert(str, length, "utf-8", "iso-8859-1",
nullptr, &written, nullptr); nullptr, &written, nullptr);
if (temp != nullptr) if (temp != nullptr) {
/* success! */ /* success! */
return temp; char *p = xstrdup(temp);
g_free(temp);
return p;
}
/* no, still broken - there's no medication, just patch /* no, still broken - there's no medication, just patch
invalid sequences */ invalid sequences */
...@@ -96,7 +101,7 @@ clear_non_printable(const char *p, size_t length) ...@@ -96,7 +101,7 @@ clear_non_printable(const char *p, size_t length)
if (first == nullptr) if (first == nullptr)
return nullptr; return nullptr;
dest = g_strndup(p, length); dest = xstrndup(p, length);
for (size_t i = first - p; i < length; ++i) for (size_t i = first - p; i < length; ++i)
if (char_is_non_printable(dest[i])) if (char_is_non_printable(dest[i]))
...@@ -120,7 +125,7 @@ FixTagString(const char *p, size_t length) ...@@ -120,7 +125,7 @@ FixTagString(const char *p, size_t length)
if (cleared == nullptr) if (cleared == nullptr)
cleared = utf8; cleared = utf8;
else else
g_free(utf8); free(utf8);
return cleared; return cleared;
} }
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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.
*/
#include "Alloc.hxx"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
gcc_noreturn
static void
oom()
{
(void)write(STDERR_FILENO, "Out of memory\n", 14);
_exit(1);
}
void *
xalloc(size_t size)
{
void *p = malloc(size);
if (gcc_unlikely(p == nullptr))
oom();
return p;
}
void *
xmemdup(const void *s, size_t size)
{
void *p = xalloc(size);
memcpy(p, s, size);
return p;
}
char *
xstrdup(const char *s)
{
char *p = strdup(s);
if (gcc_unlikely(p == nullptr))
oom();
return p;
}
char *
xstrndup(const char *s, size_t n)
{
#ifdef WIN32
char *p = (char *)xalloc(n + 1);
memcpy(p, s, n);
p[n] = 0;
#else
char *p = strndup(s, n);
if (gcc_unlikely(p == nullptr))
oom();
#endif
return p;
}
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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 MPD_ALLOC_HXX
#define MPD_ALLOC_HXX
#include "Compiler.h"
#include <stddef.h>
/**
* Allocate memory. Use free() to free it.
*
* This function never fails; in out-of-memory situations, it aborts
* the process.
*/
gcc_malloc
void *
xalloc(size_t size);
/**
* Duplicate memory. Use free() to free it.
*
* This function never fails; in out-of-memory situations, it aborts
* the process.
*/
gcc_malloc gcc_nonnull_all
void *
xmemdup(const void *s, size_t size);
/**
* Duplicate a string. Use free() to free it.
*
* This function never fails; in out-of-memory situations, it aborts
* the process.
*/
gcc_malloc gcc_nonnull_all
char *
xstrdup(const char *s);
/**
* Duplicate a string. Use free() to free it.
*
* This function never fails; in out-of-memory situations, it aborts
* the process.
*/
gcc_malloc gcc_nonnull_all
char *
xstrndup(const char *s, size_t n);
#endif
...@@ -30,14 +30,13 @@ ...@@ -30,14 +30,13 @@
#ifndef MPD_VAR_SIZE_HXX #ifndef MPD_VAR_SIZE_HXX
#define MPD_VAR_SIZE_HXX #define MPD_VAR_SIZE_HXX
#include "Alloc.hxx"
#include "Compiler.h" #include "Compiler.h"
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include <new> #include <new>
#include <glib.h>
/** /**
* Allocate and construct a variable-size object. That is useful for * Allocate and construct a variable-size object. That is useful for
* example when you want to store a variable-length string as the last * example when you want to store a variable-length string as the last
...@@ -61,7 +60,7 @@ NewVarSize(size_t declared_tail_size, size_t real_tail_size, Args&&... args) ...@@ -61,7 +60,7 @@ NewVarSize(size_t declared_tail_size, size_t real_tail_size, Args&&... args)
size_t size = sizeof(T) - declared_tail_size + real_tail_size; size_t size = sizeof(T) - declared_tail_size + real_tail_size;
/* allocate memory */ /* allocate memory */
T *instance = (T *)g_malloc(size); T *instance = (T *)xalloc(size);
/* call the constructor */ /* call the constructor */
new(instance) T(std::forward<Args>(args)...); new(instance) T(std::forward<Args>(args)...);
...@@ -78,7 +77,7 @@ DeleteVarSize(T *instance) ...@@ -78,7 +77,7 @@ DeleteVarSize(T *instance)
instance->T::~T(); instance->T::~T();
/* free memory */ /* free memory */
g_free(instance); free(instance);
} }
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment