mturk 2005/02/12 03:32:13 Modified: jk/native/common jk_lb_worker.c jk_shm.c jk_shm.h Log: Simplify shared memory. If shmem is not defined use plain jk_pool instead. Revision Changes Path 1.44 +5 -5 jakarta-tomcat-connectors/jk/native/common/jk_lb_worker.c Index: jk_lb_worker.c =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/jk/native/common/jk_lb_worker.c,v retrieving revision 1.43 retrieving revision 1.44 diff -u -r1.43 -r1.44 --- jk_lb_worker.c 9 Feb 2005 12:34:13 -0000 1.43 +++ jk_lb_worker.c 12 Feb 2005 11:32:12 -0000 1.44 @@ -56,7 +56,7 @@ { jk_worker_t *w; /* Shared memory worker data */ - jk_shm_w_rec_t *s; + jk_shm_worker_t *s; }; typedef struct worker_record worker_record_t; @@ -74,7 +74,7 @@ JK_CRIT_SEC cs; /* Shared memory worker data */ - jk_shm_w_rec_t *s; + jk_shm_worker_t *s; }; typedef struct lb_worker lb_worker_t; @@ -575,7 +575,7 @@ num_of_workers * sizeof(worker_record_t)); for (i = 0; i < num_of_workers; i++) { - p->lb_workers[i].s = jk_shm_worker_alloc(); + p->lb_workers[i].s = jk_shm_alloc(&p->p, sizeof(jk_shm_worker_t)); if (p->lb_workers[i].s == NULL) { JK_TRACE_EXIT(l); return JK_FALSE; @@ -776,7 +776,7 @@ private_data->buf, sizeof(jk_pool_atom_t) * TINY_POOL_SIZE); - private_data->s = jk_shm_worker_alloc(); + private_data->s = jk_shm_alloc(&private_data->p, sizeof(jk_shm_worker_t)); if (!private_data->s) { free(private_data); JK_TRACE_EXIT(l); 1.8 +29 -60 jakarta-tomcat-connectors/jk/native/common/jk_shm.c Index: jk_shm.c =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/jk/native/common/jk_shm.c,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- jk_shm.c 7 Feb 2005 19:06:34 -0000 1.7 +++ jk_shm.c 12 Feb 2005 11:32:12 -0000 1.8 @@ -24,16 +24,17 @@ #include "jk_shm.h" /** jk shm header record structure */ -struct jk_shm_h_rec +struct jk_shm_header { /* Shared memory magic JK_SHM_MAGIC */ - char magic[8]; - int workers; - int allocated; - jk_shm_w_rec_t *pr[1]; - jk_shm_w_rec_t rr[1]; + char magic[8]; + size_t size; + size_t pos; + int childs; + char buf[1]; }; -typedef struct jk_shm_h_rec jk_shm_h_rec_t; + +typedef struct jk_shm_header jk_shm_header_t; /** jk shm structure */ struct jk_shm @@ -42,13 +43,13 @@ const char *filename; int fd; int attached; - jk_shm_h_rec_t *hdr; + jk_shm_header_t *hdr; }; typedef struct jk_shm jk_shm_t; static const char shm_signature[] = { JK_SHM_MAGIC }; -static jk_shm_t jk_shmem = { 0, NULL, -1, 0, NULL};; +static jk_shm_t jk_shmem = { 0, NULL, -1, 0, NULL}; #if defined (WIN32) || defined(NETWARE) @@ -59,17 +60,16 @@ return 0; } - jk_shmem.size = JK_SHM_ALIGN(sizeof(jk_shm_h_rec_t) + JK_SHM_MAX_WORKERS * sizeof(jk_shm_w_rec_t)); + jk_shmem.size = JK_SHM_ALIGN(sizeof(jk_shm_header_t) + JK_SHM_SIZE); - jk_shmem.hdr = (jk_shm_h_rec_t *)calloc(1, jk_shmem.size); + jk_shmem.hdr = (jk_shm_header_t *)calloc(1, jk_shmem.size); if (!jk_shmem.hdr) return -1; jk_shmem.filename = "memory"; jk_shmem.fd = 0; jk_shmem.attached = 0; - memcpy(jk_shmem.hdr->magic, shm_signature, 8); - jk_shmem.hdr->workers = JK_SHM_MAX_WORKERS; + jk_shmem.hdr->size = JK_SHM_SIZE; return 0; } @@ -77,6 +77,7 @@ { if (!jk_shm_open(fname)) { jk_shmem.attached = 1; + jk_shmem.hdr->childs++; return 0; } else @@ -122,18 +123,11 @@ /* Use plain memory in case there is no file name */ if (!fname) { - jk_shmem.size = JK_SHM_ALIGN(sizeof(jk_shm_h_rec_t) + JK_SHM_MAX_WORKERS * sizeof(jk_shm_w_rec_t *)); - jk_shmem.hdr = calloc(1, jk_shmem.size); - if (!jk_shmem.hdr) - return -1; - memcpy(jk_shmem.hdr->magic, shm_signature, 8); - jk_shmem.hdr->workers = JK_SHM_MAX_WORKERS; - jk_shmem.hdr->allocated = 0; - jk_shmem.filename = "memory"; + jk_shmem.filename = "memory"; return 0; } - jk_shmem.size = JK_SHM_ALIGN(sizeof(jk_shm_h_rec_t) + JK_SHM_MAX_WORKERS * sizeof(jk_shm_w_rec_t)); + jk_shmem.size = JK_SHM_ALIGN(sizeof(jk_shm_header_t) + JK_SHM_SIZE); if (!attached) flags |= (O_CREAT|O_TRUNC); @@ -179,9 +173,10 @@ if (!attached) { memset(jk_shmem.hdr, 0, jk_shmem.size); memcpy(jk_shmem.hdr->magic, shm_signature, 8); - jk_shmem.hdr->workers = JK_SHM_MAX_WORKERS; + jk_shmem.hdr->size = JK_SHM_SIZE; } else { + jk_shmem.hdr->childs++; /* TODO: check header magic */ } return 0; @@ -204,14 +199,6 @@ munmap((void *)jk_shmem.hdr, jk_shmem.size); close(jk_shmem.fd); } - else { - int i; - for (i = 0; i < jk_shmem.hdr->allocated; i++) { - if (jk_shmem.hdr->pr[i]) - free(jk_shmem.hdr->pr[i]); - } - free(jk_shmem.hdr); - } } jk_shmem.hdr = NULL; jk_shmem.fd = -1; @@ -220,39 +207,21 @@ #endif -jk_shm_w_rec_t *jk_shm_worker(int id) +void *jk_shm_alloc(jk_pool_t *p, size_t size) { - jk_shm_w_rec_t *w = NULL; - if (jk_shmem.hdr && id >= 0) { - if (id < jk_shmem.hdr->allocated) { - if (jk_shmem.fd >= 0) - w = &(jk_shmem.hdr->rr[id]); - else - w = jk_shmem.hdr->pr[id]; - if (id != w->id) - w = NULL; - } - } - return w; -} + void *rc = NULL; -jk_shm_w_rec_t *jk_shm_worker_alloc() -{ - jk_shm_w_rec_t *w = NULL; if (jk_shmem.hdr) { - if (jk_shmem.hdr->allocated < jk_shmem.hdr->workers) { - jk_shmem.hdr->allocated++; - if (jk_shmem.fd >= 0) { - w = &(jk_shmem.hdr->rr[jk_shmem.hdr->allocated]); - } - else { - jk_shmem.hdr->pr[jk_shmem.hdr->allocated] = calloc(1, sizeof(jk_shm_w_rec_t)); - w = jk_shmem.hdr->pr[jk_shmem.hdr->allocated]; - } - w->id = jk_shmem.hdr->allocated; + size = JK_ALIGN_DEFAULT(size); + if ((jk_shmem.hdr->size - jk_shmem.hdr->pos) >= size) { + rc = &(jk_shmem.hdr->buf[jk_shmem.hdr->pos]); + jk_shmem.hdr->pos += size; } } - return w; + else if (p) + rc = jk_pool_alloc(p, size); + + return rc; } const char *jk_shm_name() 1.6 +7 -11 jakarta-tomcat-connectors/jk/native/common/jk_shm.h Index: jk_shm.h =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/jk/native/common/jk_shm.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- jk_shm.h 9 Feb 2005 12:33:13 -0000 1.5 +++ jk_shm.h 12 Feb 2005 11:32:12 -0000 1.6 @@ -45,11 +45,11 @@ /* Really huge numbers, but 512 workers should be enough */ #define JK_SHM_MAX_WORKERS 512 - +#define JK_SHM_SIZE (1024 * 1024) #define JK_SHM_ALIGN(x) JK_ALIGN(x, 1024) /** jk shm worker record structure */ -struct jk_shm_w_rec +struct jk_shm_worker { int id; /* Number of currently busy channels */ @@ -88,7 +88,7 @@ /* Number of non 200 responses */ size_t errors; }; -typedef struct jk_shm_w_rec jk_shm_w_rec_t; +typedef struct jk_shm_worker jk_shm_worker_t; const char *jk_shm_name(); @@ -105,14 +105,10 @@ */ int jk_shm_attach(const char *fname); - -/* Return shm worker record - */ -jk_shm_w_rec_t *jk_shm_worker(int id); - -/* allocate shm worker record +/* allocate shm memory + * If there is no shm present the pool will be used instead */ -jk_shm_w_rec_t *jk_shm_worker_alloc(); +void *jk_shm_alloc(jk_pool_t *p, size_t size); #ifdef __cplusplus
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]