Skip to content

Commit

Permalink
CLEANUP: Move event callback functions to mc_util files
Browse files Browse the repository at this point in the history
  • Loading branch information
ing-eoking authored and jhpark816 committed Mar 5, 2024
1 parent a7ecf08 commit f8e9de7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
40 changes: 40 additions & 0 deletions mc_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,43 @@ ENGINE_ERROR_CODE tokenize_sblocks(mblck_list_t *blist, int keylen, int keycnt,
return tokenize_mblocks(blist, keylen-2, keycnt, maxklen,
must_backward_compatible, tokens);
}

/*
* event callback functions
*/

/* event handlers structure */
static struct engine_event_handler {
EVENT_CALLBACK cb;
const void *cb_data;
struct engine_event_handler *next;
} *engine_event_handlers[MAX_ENGINE_EVENT_TYPE + 1];

/*
* Register a callback.
*/
void register_callback(ENGINE_HANDLE *eh,
ENGINE_EVENT_TYPE type,
EVENT_CALLBACK cb, const void *cb_data)
{
struct engine_event_handler *h =
calloc(sizeof(struct engine_event_handler), 1);

assert(h);
h->cb = cb;
h->cb_data = cb_data;
h->next = engine_event_handlers[type];
engine_event_handlers[type] = h;
}

/*
* Perform all callbacks of a given type for the given connection.
*/
void perform_callbacks(ENGINE_EVENT_TYPE type,
const void *data, const void *c)
{
for (struct engine_event_handler *h = engine_event_handlers[type];
h; h = h->next) {
h->cb(c, type, data, h->cb_data);
}
}
8 changes: 8 additions & 0 deletions mc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef MC_UTIL_H
#define MC_UTIL_H

#include <memcached/engine.h>
#include <memcached/extension.h>

/* length of string representing 4 bytes integer is 10 */
Expand Down Expand Up @@ -95,4 +96,11 @@ ENGINE_ERROR_CODE tokenize_sblocks(mblck_list_t *blist, int keylen, int keycnt,
int maxklen, bool must_backward_compatible,
token_t *tokens);

/* event callback functions */
void register_callback(ENGINE_HANDLE *eh,
ENGINE_EVENT_TYPE type,
EVENT_CALLBACK cb, const void *cb_data);
void perform_callbacks(ENGINE_EVENT_TYPE type,
const void *data, const void *c);

#endif
27 changes: 0 additions & 27 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ static struct event_base *main_base;
struct thread_stats *default_thread_stats;
topkeys_t *default_topkeys = NULL;

static struct engine_event_handler *engine_event_handlers[MAX_ENGINE_EVENT_TYPE + 1];

#ifdef ENABLE_ZK_INTEGRATION
static char *arcus_zk_cfg = NULL;
#endif
Expand Down Expand Up @@ -443,31 +441,6 @@ void safe_close(int sfd)
}
}

// Register a callback.
static void register_callback(ENGINE_HANDLE *eh,
ENGINE_EVENT_TYPE type,
EVENT_CALLBACK cb, const void *cb_data)
{
struct engine_event_handler *h =
calloc(sizeof(struct engine_event_handler), 1);

assert(h);
h->cb = cb;
h->cb_data = cb_data;
h->next = engine_event_handlers[type];
engine_event_handlers[type] = h;
}

// Perform all callbacks of a given type for the given connection.
static void perform_callbacks(ENGINE_EVENT_TYPE type,
const void *data, const void *c)
{
for (struct engine_event_handler *h = engine_event_handlers[type];
h; h = h->next) {
h->cb(c, type, data, h->cb_data);
}
}

/*
* Free list management for connections.
*/
Expand Down
6 changes: 0 additions & 6 deletions memcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,6 @@ struct settings {
} extensions;
};

struct engine_event_handler {
EVENT_CALLBACK cb;
const void *cb_data;
struct engine_event_handler *next;
};

extern struct settings settings;
extern EXTENSION_LOGGER_DESCRIPTOR *mc_logger;

Expand Down

0 comments on commit f8e9de7

Please sign in to comment.