apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rich Yonts <richyo...@gmail.com>
Subject dynamic memory at runtime
Date Fri, 22 Jan 2010 21:36:30 GMT
I am trying to dynamically obtain memory from a pool at runtime.  No matter
how I tweak it, it does not seem to work correctly with multiple processes.
I have some code below that ouputs something odd--this is one of numerous
different tries, please do not worry too much about clever (or lack thereof
:-)) programming (all error checking intentionally left out).  I want to
know how to create lists at runtime.  They are ultimately going to store
URLs and some statistics about them, such as count, latency, etc.  The goal
is to have a dynamic structure that keeps track of every URL requested.
This, of course, needs to be shared across each process so there is only one
list per Apache instance.  Would you look at the code and suggest why my
list is not working?  Where I have a for loop, I have used a while but got
an infinite loop.  This means that the update of the pointer is not
happening.  I have done this so much that I am no longer thinking clearly
about it and my eyes are bleary.  Does anyone have such a dynamic
structure?  I can create nice ones in post_config and use them during
runtime, but that does not allow me to build lists while running.  All help
appreciated.  Thanks.

Here is the output showing that the next pointer does not seem to be
working, yet it has something in it:

[Fri Jan 22 13:18:20 2010] [notice] Apache/2.2.13 (Unix) configured --
resuming normal operations
[Fri Jan 22 13:18:23 2010] [error] [client] node is 0 29418
[Fri Jan 22 13:18:25 2010] [error] [client] node is 1 29419
[Fri Jan 22 13:18:25 2010] [error] [client] node is 1 29419
[Fri Jan 22 13:18:27 2010] [error] [client] node is 2 29420
[Fri Jan 22 13:18:27 2010] [error] [client] node is 2 29420
[Fri Jan 22 13:18:27 2010] [error] [client] node is 2 29420

Here is the code with the obvious stuff missing (#includes,

struct List {
    apr_shm_t* head;
    apr_pool_t* pool;
    int   count;
struct Node {
    int   number;
    apr_shm_t* next;
apr_shm_t*  shared_list;

static int log_transaction(request_rec* request) {  // create LIFO list
    Node*  node;
    List*  list;
    apr_shm_t* temp;

    list = (List*)apr_shm_baseaddr_get(shared_list);
    temp = list->head;
    apr_shm_create(&list->head, sizeof(Node), NULL, list->pool);  // create
new node as head
    node = (Node*)apr_shm_baseaddr_get(list->head);  // get this processes'
handle to the node
    node->number = list->count++;
    node->next = temp;
    temp = list->head;  // get head node
    for (int i = 0; i < list->count; i++) {  // spin through showing all
        node = (Node*)apr_shm_baseaddr_get(temp);  // get this processes'
handle to the node
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "node is %d %ld",
node->number, (long int)getpid());
        temp = node->next;  // get next node
    return DECLINED;

static int post_config(apr_pool_t* pool, apr_pool_t* log, apr_pool_t* temp,
server_rec* server) {
    void*  data;
    List*  list;
    apr_pool_userdata_get(&data, "mod_dynamic", server->process->pool);
    if (!data) {
        apr_pool_userdata_set((const void*)1, "mod_dynamic",
apr_pool_cleanup_null, server->process->pool);
        return OK;
    apr_shm_create(&shared_list, sizeof(List), NULL, pool);
    list = (List*)apr_shm_baseaddr_get(shared_list);  // create list
    list->head = NULL;  // start with empty list
    list->pool = pool;
    list->count = 0;
    return OK;

View raw message