allocate strings in a pool (taken from ncmpc)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
bin_PROGRAMS = ncmpcpp
|
||||
ncmpcpp_SOURCES = color_parser.cpp help.cpp helpers.cpp libmpdclient.c \
|
||||
lyrics.cpp menu.cpp misc.cpp mpdpp.cpp ncmpcpp.cpp scrollpad.cpp search_engine.cpp \
|
||||
settings.cpp song.cpp status_checker.cpp tag_editor.cpp window.cpp
|
||||
settings.cpp song.cpp status_checker.cpp str_pool.c tag_editor.cpp window.cpp
|
||||
|
||||
# set the include path found by configure
|
||||
INCLUDES= $(all_includes)
|
||||
|
||||
@@ -295,15 +295,15 @@ static mpd_ReturnElement * mpd_newReturnElement(const char * name, const char *
|
||||
{
|
||||
mpd_ReturnElement * ret = malloc(sizeof(mpd_ReturnElement));
|
||||
|
||||
ret->name = strdup(name);
|
||||
ret->value = strdup(value);
|
||||
ret->name = str_pool_get(name);
|
||||
ret->value = str_pool_get(value);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void mpd_freeReturnElement(mpd_ReturnElement * re) {
|
||||
free(re->name);
|
||||
free(re->value);
|
||||
str_pool_put(re->name);
|
||||
str_pool_put(re->value);
|
||||
free(re);
|
||||
}
|
||||
|
||||
@@ -940,18 +940,18 @@ static void mpd_initSong(mpd_Song * song) {
|
||||
}
|
||||
|
||||
static void mpd_finishSong(mpd_Song * song) {
|
||||
if(song->file) free(song->file);
|
||||
if(song->artist) free(song->artist);
|
||||
if(song->album) free(song->album);
|
||||
if(song->title) free(song->title);
|
||||
if(song->track) free(song->track);
|
||||
if(song->name) free(song->name);
|
||||
if(song->date) free(song->date);
|
||||
if(song->genre) free(song->genre);
|
||||
if(song->composer) free(song->composer);
|
||||
if(song->performer) free(song->performer);
|
||||
if(song->disc) free(song->disc);
|
||||
if(song->comment) free(song->comment);
|
||||
if(song->file) str_pool_put(song->file);
|
||||
if(song->artist) str_pool_put(song->artist);
|
||||
if(song->album) str_pool_put(song->album);
|
||||
if(song->title) str_pool_put(song->title);
|
||||
if(song->track) str_pool_put(song->track);
|
||||
if(song->name) str_pool_put(song->name);
|
||||
if(song->date) str_pool_put(song->date);
|
||||
if(song->genre) str_pool_put(song->genre);
|
||||
if(song->composer) str_pool_put(song->composer);
|
||||
if(song->performer) str_pool_put(song->performer);
|
||||
if(song->disc) str_pool_put(song->disc);
|
||||
if(song->comment) str_pool_put(song->comment);
|
||||
}
|
||||
|
||||
mpd_Song * mpd_newSong(void) {
|
||||
@@ -970,18 +970,18 @@ void mpd_freeSong(mpd_Song * song) {
|
||||
mpd_Song * mpd_songDup(mpd_Song * song) {
|
||||
mpd_Song * ret = mpd_newSong();
|
||||
|
||||
if(song->file) ret->file = strdup(song->file);
|
||||
if(song->artist) ret->artist = strdup(song->artist);
|
||||
if(song->album) ret->album = strdup(song->album);
|
||||
if(song->title) ret->title = strdup(song->title);
|
||||
if(song->track) ret->track = strdup(song->track);
|
||||
if(song->name) ret->name = strdup(song->name);
|
||||
if(song->date) ret->date = strdup(song->date);
|
||||
if(song->genre) ret->genre= strdup(song->genre);
|
||||
if(song->composer) ret->composer= strdup(song->composer);
|
||||
if(song->performer) ret->performer = strdup(song->performer);
|
||||
if(song->disc) ret->disc = strdup(song->disc);
|
||||
if(song->comment) ret->comment = strdup(song->comment);
|
||||
if(song->file) ret->file = str_pool_dup(song->file);
|
||||
if(song->artist) ret->artist = str_pool_dup(song->artist);
|
||||
if(song->album) ret->album = str_pool_dup(song->album);
|
||||
if(song->title) ret->title = str_pool_dup(song->title);
|
||||
if(song->track) ret->track = str_pool_dup(song->track);
|
||||
if(song->name) ret->name = str_pool_dup(song->name);
|
||||
if(song->date) ret->date = str_pool_dup(song->date);
|
||||
if(song->genre) ret->genre= str_pool_dup(song->genre);
|
||||
if(song->composer) ret->composer= str_pool_dup(song->composer);
|
||||
if(song->performer) ret->performer = str_pool_dup(song->performer);
|
||||
if(song->disc) ret->disc = str_pool_dup(song->disc);
|
||||
if(song->comment) ret->comment = str_pool_dup(song->comment);
|
||||
ret->time = song->time;
|
||||
ret->pos = song->pos;
|
||||
ret->id = song->id;
|
||||
@@ -994,7 +994,8 @@ static void mpd_initDirectory(mpd_Directory * directory) {
|
||||
}
|
||||
|
||||
static void mpd_finishDirectory(mpd_Directory * directory) {
|
||||
if(directory->path) free(directory->path);
|
||||
if(directory->path)
|
||||
str_pool_put(directory->path);
|
||||
}
|
||||
|
||||
mpd_Directory * mpd_newDirectory(void) {
|
||||
@@ -1014,7 +1015,8 @@ void mpd_freeDirectory(mpd_Directory * directory) {
|
||||
mpd_Directory * mpd_directoryDup(mpd_Directory * directory) {
|
||||
mpd_Directory * ret = mpd_newDirectory();
|
||||
|
||||
if(directory->path) ret->path = strdup(directory->path);
|
||||
if(directory->path)
|
||||
ret->path = str_pool_dup(directory->path);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1024,7 +1026,8 @@ static void mpd_initPlaylistFile(mpd_PlaylistFile * playlist) {
|
||||
}
|
||||
|
||||
static void mpd_finishPlaylistFile(mpd_PlaylistFile * playlist) {
|
||||
if(playlist->path) free(playlist->path);
|
||||
if(playlist->path)
|
||||
str_pool_put(playlist->path);
|
||||
}
|
||||
|
||||
mpd_PlaylistFile * mpd_newPlaylistFile(void) {
|
||||
@@ -1043,7 +1046,8 @@ void mpd_freePlaylistFile(mpd_PlaylistFile * playlist) {
|
||||
mpd_PlaylistFile * mpd_playlistFileDup(mpd_PlaylistFile * playlist) {
|
||||
mpd_PlaylistFile * ret = mpd_newPlaylistFile();
|
||||
|
||||
if(playlist->path) ret->path = strdup(playlist->path);
|
||||
if(playlist->path)
|
||||
ret->path = str_pool_dup(playlist->path);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1100,7 +1104,7 @@ mpd_InfoEntity * mpd_getNextInfoEntity(mpd_Connection * connection) {
|
||||
entity->type = MPD_INFO_ENTITY_TYPE_SONG;
|
||||
entity->info.song = mpd_newSong();
|
||||
entity->info.song->file =
|
||||
strdup(connection->returnElement->value);
|
||||
str_pool_dup(connection->returnElement->value);
|
||||
}
|
||||
else if(strcmp(connection->returnElement->name,
|
||||
"directory")==0) {
|
||||
@@ -1108,14 +1112,14 @@ mpd_InfoEntity * mpd_getNextInfoEntity(mpd_Connection * connection) {
|
||||
entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
|
||||
entity->info.directory = mpd_newDirectory();
|
||||
entity->info.directory->path =
|
||||
strdup(connection->returnElement->value);
|
||||
str_pool_dup(connection->returnElement->value);
|
||||
}
|
||||
else if(strcmp(connection->returnElement->name,"playlist")==0) {
|
||||
entity = mpd_newInfoEntity();
|
||||
entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
|
||||
entity->info.playlistFile = mpd_newPlaylistFile();
|
||||
entity->info.playlistFile->path =
|
||||
strdup(connection->returnElement->value);
|
||||
str_pool_dup(connection->returnElement->value);
|
||||
}
|
||||
else if(strcmp(connection->returnElement->name, "cpos") == 0){
|
||||
entity = mpd_newInfoEntity();
|
||||
@@ -1144,23 +1148,23 @@ mpd_InfoEntity * mpd_getNextInfoEntity(mpd_Connection * connection) {
|
||||
strlen(re->value)) {
|
||||
if(!entity->info.song->artist &&
|
||||
strcmp(re->name,"Artist")==0) {
|
||||
entity->info.song->artist = strdup(re->value);
|
||||
entity->info.song->artist = str_pool_dup(re->value);
|
||||
}
|
||||
else if(!entity->info.song->album &&
|
||||
strcmp(re->name,"Album")==0) {
|
||||
entity->info.song->album = strdup(re->value);
|
||||
entity->info.song->album = str_pool_dup(re->value);
|
||||
}
|
||||
else if(!entity->info.song->title &&
|
||||
strcmp(re->name,"Title")==0) {
|
||||
entity->info.song->title = strdup(re->value);
|
||||
entity->info.song->title = str_pool_dup(re->value);
|
||||
}
|
||||
else if(!entity->info.song->track &&
|
||||
strcmp(re->name,"Track")==0) {
|
||||
entity->info.song->track = strdup(re->value);
|
||||
entity->info.song->track = str_pool_dup(re->value);
|
||||
}
|
||||
else if(!entity->info.song->name &&
|
||||
strcmp(re->name,"Name")==0) {
|
||||
entity->info.song->name = strdup(re->value);
|
||||
entity->info.song->name = str_pool_dup(re->value);
|
||||
}
|
||||
else if(entity->info.song->time==MPD_SONG_NO_TIME &&
|
||||
strcmp(re->name,"Time")==0) {
|
||||
@@ -1176,27 +1180,27 @@ mpd_InfoEntity * mpd_getNextInfoEntity(mpd_Connection * connection) {
|
||||
}
|
||||
else if(!entity->info.song->date &&
|
||||
strcmp(re->name, "Date") == 0) {
|
||||
entity->info.song->date = strdup(re->value);
|
||||
entity->info.song->date = str_pool_dup(re->value);
|
||||
}
|
||||
else if(!entity->info.song->genre &&
|
||||
strcmp(re->name, "Genre") == 0) {
|
||||
entity->info.song->genre = strdup(re->value);
|
||||
entity->info.song->genre = str_pool_dup(re->value);
|
||||
}
|
||||
else if(!entity->info.song->composer &&
|
||||
strcmp(re->name, "Composer") == 0) {
|
||||
entity->info.song->composer = strdup(re->value);
|
||||
entity->info.song->composer = str_pool_dup(re->value);
|
||||
}
|
||||
else if(!entity->info.song->performer &&
|
||||
strcmp(re->name, "Performer") == 0) {
|
||||
entity->info.song->performer = strdup(re->value);
|
||||
entity->info.song->performer = str_pool_dup(re->value);
|
||||
}
|
||||
else if(!entity->info.song->disc &&
|
||||
strcmp(re->name, "Disc") == 0) {
|
||||
entity->info.song->disc = strdup(re->value);
|
||||
entity->info.song->disc = str_pool_dup(re->value);
|
||||
}
|
||||
else if(!entity->info.song->comment &&
|
||||
strcmp(re->name, "Comment") == 0) {
|
||||
entity->info.song->comment = strdup(re->value);
|
||||
entity->info.song->comment = str_pool_dup(re->value);
|
||||
}
|
||||
}
|
||||
else if(entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
# define __W32API_USE_DLLIMPORT__ 1
|
||||
#endif
|
||||
|
||||
#include "str_pool.h"
|
||||
#include <sys/time.h>
|
||||
#include <stdarg.h>
|
||||
#define MPD_BUFFER_MAX_LENGTH 50000
|
||||
|
||||
125
src/str_pool.c
Normal file
125
src/str_pool.c
Normal file
@@ -0,0 +1,125 @@
|
||||
/* ncmpc
|
||||
* Copyright (C) 2008 Max Kellermann <max@duempel.org>
|
||||
* This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "str_pool.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define NUM_SLOTS 4096
|
||||
|
||||
struct slot {
|
||||
struct slot *next;
|
||||
unsigned char ref;
|
||||
char value[1];
|
||||
} __attribute__((packed));
|
||||
|
||||
struct slot *slots[NUM_SLOTS];
|
||||
|
||||
static inline unsigned
|
||||
calc_hash(const char *p)
|
||||
{
|
||||
unsigned hash = 5381;
|
||||
|
||||
assert(p != NULL);
|
||||
|
||||
while (*p != 0)
|
||||
hash = (hash << 5) + hash + *p++;
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
static inline struct slot *
|
||||
value_to_slot(char *value)
|
||||
{
|
||||
return (struct slot*)(value - offsetof(struct slot, value));
|
||||
}
|
||||
|
||||
static struct slot *slot_alloc(struct slot *next, const char *value)
|
||||
{
|
||||
size_t length = strlen(value);
|
||||
struct slot *slot = malloc(sizeof(*slot) + length);
|
||||
if (slot == NULL)
|
||||
abort(); /* XXX */
|
||||
|
||||
slot->next = next;
|
||||
slot->ref = 1;
|
||||
memcpy(slot->value, value, length + 1);
|
||||
return slot;
|
||||
}
|
||||
|
||||
char *str_pool_get(const char *value)
|
||||
{
|
||||
struct slot **slot_p, *slot;
|
||||
|
||||
slot_p = &slots[calc_hash(value) % NUM_SLOTS];
|
||||
for (slot = *slot_p; slot != NULL; slot = slot->next) {
|
||||
if (strcmp(value, slot->value) == 0 && slot->ref < 0xff) {
|
||||
assert(slot->ref > 0);
|
||||
++slot->ref;
|
||||
return slot->value;
|
||||
}
|
||||
}
|
||||
|
||||
slot = slot_alloc(*slot_p, value);
|
||||
*slot_p = slot;
|
||||
return slot->value;
|
||||
}
|
||||
|
||||
char *str_pool_dup(char *value)
|
||||
{
|
||||
struct slot *slot = value_to_slot(value);
|
||||
|
||||
assert(slot->ref > 0);
|
||||
|
||||
if (slot->ref < 0xff) {
|
||||
++slot->ref;
|
||||
return value;
|
||||
} else {
|
||||
/* the reference counter overflows above 0xff;
|
||||
duplicate the value, and start with 1 */
|
||||
struct slot **slot_p =
|
||||
&slots[calc_hash(slot->value) % NUM_SLOTS];
|
||||
slot = slot_alloc(*slot_p, slot->value);
|
||||
*slot_p = slot;
|
||||
return slot->value;
|
||||
}
|
||||
}
|
||||
|
||||
void str_pool_put(char *value)
|
||||
{
|
||||
struct slot **slot_p, *slot;
|
||||
|
||||
slot = value_to_slot(value);
|
||||
assert(slot->ref > 0);
|
||||
--slot->ref;
|
||||
|
||||
if (slot->ref > 0)
|
||||
return;
|
||||
|
||||
for (slot_p = &slots[calc_hash(value) % NUM_SLOTS];
|
||||
*slot_p != slot;
|
||||
slot_p = &(*slot_p)->next) {
|
||||
assert(*slot_p != NULL);
|
||||
}
|
||||
|
||||
*slot_p = slot->next;
|
||||
free(slot);
|
||||
}
|
||||
37
src/str_pool.h
Normal file
37
src/str_pool.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/* ncmpc
|
||||
* Copyright (C) 2008 Max Kellermann <max@duempel.org>
|
||||
* This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef STR_POOL_H
|
||||
#define STR_POOL_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
char *str_pool_get(const char *value);
|
||||
|
||||
char *str_pool_dup(char *value);
|
||||
|
||||
void str_pool_put(char *value);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user