Skip to content

Commit

Permalink
MB-5567: Release allocated memory during shutdown
Browse files Browse the repository at this point in the history
Change-Id: Ic64e0b393caa2bc4588881690e408d58366be576
Reviewed-on: http://review.couchbase.org/17265
Reviewed-by: Dustin Sallings <dustin@spy.net>
Tested-by: Trond Norbye <trond.norbye@gmail.com>
  • Loading branch information
trondn committed Jun 15, 2012
1 parent 1764fc2 commit a7ff825
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions daemon/cache.c
Expand Up @@ -77,6 +77,7 @@ void cache_destroy(cache_t *cache) {
free(cache->name);
free(cache->ptr);
pthread_mutex_destroy(&cache->mutex);
free(cache);
}

void* cache_alloc(cache_t *cache) {
Expand Down
21 changes: 20 additions & 1 deletion daemon/memcached.c
Expand Up @@ -592,6 +592,19 @@ static void initialize_connections(void)
}
}

static void destroy_connections(void)
{
for (int ii = 0; ii < settings.maxconns; ++ii) {
if (connections.all[ii]) {
conn *c = connections.all[ii];
conn_destructor(c);
free(c);
}
}

free(connections.all);
}

static conn *allocate_connection(void) {
conn *ret;

Expand Down Expand Up @@ -7761,7 +7774,7 @@ int main (int argc, char **argv) {
}

/* initialize main thread libevent instance */
main_base = event_init();
main_base = event_base_new();

/* Load the storage engine */
ENGINE_HANDLE *engine_handle = NULL;
Expand Down Expand Up @@ -7899,5 +7912,11 @@ int main (int argc, char **argv) {
free(stats.listening_ports);
}

event_base_free(main_base);
release_independent_stats(default_independent_stats);
destroy_connections();

unload_engine();

return EXIT_SUCCESS;
}
13 changes: 12 additions & 1 deletion daemon/thread.c
Expand Up @@ -240,7 +240,7 @@ static void setup_dispatcher(struct event_base *main_base,
*/
static void setup_thread(LIBEVENT_THREAD *me) {
me->type = GENERAL;
me->base = event_init();
me->base = event_base_new();
if (! me->base) {
settings.extensions.logger->log(EXTENSION_LOG_WARNING, NULL,
"Can't allocate event base\n");
Expand Down Expand Up @@ -695,7 +695,18 @@ void threads_shutdown(void)
for (int ii = 0; ii < nthreads; ++ii) {
safe_close(threads[ii].notify[0]);
safe_close(threads[ii].notify[1]);
cache_destroy(threads[ii].suffix_cache);
event_base_free(threads[ii].base);

CQ_ITEM *it;
while ((it = cq_pop(threads[ii].new_conn_queue)) != NULL) {
cqi_free(it);
}
free(threads[ii].new_conn_queue);
}

free(thread_ids);
free(threads);
}

void notify_thread(LIBEVENT_THREAD *thread) {
Expand Down
7 changes: 7 additions & 0 deletions utilities/engine_loader.c
Expand Up @@ -17,6 +17,13 @@ static const char * const feature_descriptions[] = {

void *handle = NULL;

void unload_engine(void)
{
if (handle != NULL) {
dlclose(handle);
}
}

bool load_engine(const char *soname,
SERVER_HANDLE_V1 *(*get_server_api)(void),
EXTENSION_LOGGER_DESCRIPTOR *logger,
Expand Down
2 changes: 2 additions & 0 deletions utilities/engine_loader.h
Expand Up @@ -9,6 +9,8 @@
#ifdef __cplusplus
extern "C" {
#endif
MEMCACHED_PUBLIC_API void unload_engine(void);

MEMCACHED_PUBLIC_API bool load_engine(const char *soname,
SERVER_HANDLE_V1 *(*get_server_api)(void),
EXTENSION_LOGGER_DESCRIPTOR *logger,
Expand Down

0 comments on commit a7ff825

Please sign in to comment.