Re: aiops.c and xmalloc

From: Andres Kroonmaa <andre@dont-contact.us>
Date: Thu, 5 Oct 2000 09:52:26 +0200

On 4 Oct 2000, at 20:41, Henrik Nordstrom <hno@hem.passagen.se> wrote:

> > if ((requestp->path = (char *) xmalloc(len)) == NULL) {
> >
> > AFAIK xmalloc cannot ever return with NULL, so this check
> > seems useless.
> > Is it there for some reason or is just remains since times
> > when xmalloc was not used?
>
> Purely old remains. Not needed today, but since it is there why removing
> it..

 Because its confusing, and wasting space? Could we ever stop using xmalloc?
 While I hacked it, I ripped off these checks. Do you object that?
 I'll put them back if so.

> Of the 4 xmalloc calls there is in aiops, only one is easily
> transferrable to memory pools.. (the struct stat one).

 Well, I moved all but path mallocs to Pools. Slightly higher mem usage,
 but less memory trashing on positive side. I think we can move path
 mallocs also to pools, and we can define more buftypes to get a better
 match to needed buffer sizes.
 But seems that aiops usage is very casual and low.

--- ../../squid-2.3-hno/src/aiops.c Sat Aug 19 06:47:18 2000
+++ aiops.c Wed Oct 4 16:51:59 2000
@@ -143,6 +143,16 @@
 static aio_thread_t *threads;
 static int aio_initialised = 0;
 
+#define AIO_LARGE_BUFS DISK_PAGE_SIZE
+#define AIO_MEDIUM_BUFS AIO_LARGE_BUFS >> 1
+#define AIO_SMALL_BUFS AIO_LARGE_BUFS >> 2
+#define AIO_TINY_BUFS AIO_LARGE_BUFS >> 3
+
+static MemPool *aio_large_bufs = NULL; // 16K
+static MemPool *aio_medium_bufs = NULL; // 8K
+static MemPool *aio_small_bufs = NULL; // 4K
+static MemPool *aio_tiny_bufs = NULL; // 2K
+
 static int request_queue_len = 0;
 static MemPool *aio_request_pool = NULL;
 static aio_request_t *request_queue_head = NULL;
@@ -156,6 +166,49 @@
 static struct sched_param globsched;
 static pthread_t main_thread;
 
+static MemPool *
+aio_get_pool(int size)
+{
+ MemPool *p;
+ if (size <= AIO_LARGE_BUFS) {
+ if (size <= AIO_TINY_BUFS)
+ p = aio_tiny_bufs;
+ else if (size <= AIO_SMALL_BUFS)
+ p = aio_small_bufs;
+ else if (size <= AIO_MEDIUM_BUFS)
+ p = aio_medium_bufs;
+ else
+ p = aio_large_bufs;
+ } else
+ p = NULL;
+ return p;
+}
+
+static void *
+aio_xmalloc(int size)
+{
+ void *p;
+ MemPool *pool;
+
+ if ( (pool = aio_get_pool(size)) != NULL) {
+ p = memPoolAlloc(pool);
+ } else
+ p = xmalloc(size);
+
+ return p;
+}
+
+static void
+aio_xfree(void *p, int size)
+{
+ MemPool *pool;
+
+ if ( (pool = aio_get_pool(size)) != NULL) {
+ memPoolFree(pool, p);
+ } else
+ xfree(p);
+}
+
 static void
 aio_init(void)
 {
@@ -220,6 +273,10 @@
 
     /* Create request pool */
     aio_request_pool = memPoolCreate("aio_request", sizeof(aio_request_t));
+ aio_large_bufs = memPoolCreate("aio_large_bufs", AIO_LARGE_BUFS);
+ aio_medium_bufs = memPoolCreate("aio_medium_bufs", AIO_MEDIUM_BUFS);
+ aio_small_bufs = memPoolCreate("aio_small_bufs", AIO_SMALL_BUFS);
+ aio_tiny_bufs = memPoolCreate("aio_tiny_bufs", AIO_TINY_BUFS);
 
     aio_initialised = 1;
 }
@@ -480,7 +537,7 @@
     case _AIO_OP_STAT:
         if (!cancelled && requestp->ret == 0)
             xmemcpy(requestp->statp, requestp->tmpstatp, sizeof(struct stat));
- xfree(requestp->tmpstatp);
+ aio_xfree(requestp->tmpstatp, sizeof(struct stat));
     case _AIO_OP_OPEN:
         if (cancelled && requestp->ret >= 0)
             /* The open() was cancelled but completed */
@@ -500,7 +557,7 @@
         if (!cancelled && requestp->ret > 0)
             xmemcpy(requestp->bufferp, requestp->tmpbufp, requestp->ret);
     case _AIO_OP_WRITE:
- xfree(requestp->tmpbufp);
+ aio_xfree(requestp->tmpbufp, requestp->buflen);
         break;
     default:
         break;
@@ -592,11 +649,7 @@
     }
     requestp->fd = fd;
     requestp->bufferp = bufp;
- if ((requestp->tmpbufp = (char *) xmalloc(bufs)) == NULL) {
- memPoolFree(aio_request_pool, requestp);
- errno = ENOMEM;
- return -1;
- }
+ requestp->tmpbufp = (char *) aio_xmalloc(bufs);
     requestp->buflen = bufs;
     requestp->offset = offset;
     requestp->whence = whence;
@@ -630,11 +683,7 @@
         return -1;
     }
     requestp->fd = fd;
- if ((requestp->tmpbufp = (char *) xmalloc(bufs)) == NULL) {
- memPoolFree(aio_request_pool, requestp);
- errno = ENOMEM;
- return -1;
- }
+ requestp->tmpbufp = (char *) aio_xmalloc(bufs);
     xmemcpy(requestp->tmpbufp, bufp, bufs);
     requestp->buflen = bufs;
     requestp->offset = offset;
@@ -698,19 +747,10 @@
         return -1;
     }
     len = strlen(path) + 1;
- if ((requestp->path = (char *) xmalloc(len)) == NULL) {
- memPoolFree(aio_request_pool, requestp);
- errno = ENOMEM;
- return -1;
- }
+ requestp->path = (char *) xmalloc(len);
     strncpy(requestp->path, path, len);
     requestp->statp = sb;
- if ((requestp->tmpstatp = (struct stat *) xmalloc(sizeof(struct stat))) == NULL) {
- xfree(requestp->path);
- memPoolFree(aio_request_pool, requestp);
- errno = ENOMEM;
- return -1;
- }
+ requestp->tmpstatp = (struct stat *) aio_xmalloc(sizeof(struct stat));
     requestp->resultp = resultp;
     requestp->request_type = _AIO_OP_STAT;
     requestp->cancelled = 0;
@@ -741,11 +781,7 @@
         return -1;
     }
     len = strlen(path) + 1;
- if ((requestp->path = (char *) xmalloc(len)) == NULL) {
- memPoolFree(aio_request_pool, requestp);
- errno = ENOMEM;
- return -1;
- }
+ requestp->path = (char *) xmalloc(len);
     strncpy(requestp->path, path, len);
     requestp->resultp = resultp;
     requestp->request_type = _AIO_OP_UNLINK;

------------------------------------
 Andres Kroonmaa <andre@online.ee>
 Delfi Online
 Tel: 6501 731, Fax: 6501 708
 Pärnu mnt. 158, Tallinn,
 11317 Estonia
Received on Thu Oct 05 2000 - 01:55:24 MDT

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:12:40 MST