00001 /* 00002 ** Copyright (C) 2005 by Kevin L. Mitchell <klmitch@mit.edu> 00003 ** 00004 ** This program is free software; you can redistribute it and/or modify 00005 ** it under the terms of the GNU General Public License as published by 00006 ** the Free Software Foundation; either version 2 of the License, or 00007 ** (at your option) any later version. 00008 ** 00009 ** This program is distributed in the hope that it will be useful, 00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 ** GNU General Public License for more details. 00013 ** 00014 ** You should have received a copy of the GNU General Public License 00015 ** along with this program; if not, write to the Free Software 00016 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 ** 00018 ** @(#)$Id: event_gen_alloc.c,v 1.3 2005/10/26 12:53:59 klmitch Exp $ 00019 */ 00027 #include "event_int.h" 00028 00029 #include <stdlib.h> 00030 00031 RCSTAG("@(#)$Id: event_gen_alloc.c,v 1.3 2005/10/26 12:53:59 klmitch Exp $"); 00032 00033 ev_err_t 00034 event_gen_alloc(ev_ctx_t *ctx, unsigned int gentype, void *gen) 00035 { 00036 ev_err_t err; 00037 ev_gendesc_t *gd; 00038 ev_genhdr_t *hdr; 00039 00040 ev_init(); /* make sure library is initialized... */ 00041 00042 if (!ec_verify(ctx) || !gentype || !gen) /* sanity-check arguments */ 00043 ev_return(EINVAL); 00044 00045 /* OK, first, let's look up the descriptor for the generator... */ 00046 if ((err = _ev_gen_lookup(ctx, gentype, &gd))) 00047 ev_return(err); 00048 00049 /* Next, let's pop one off the free list if possible... */ 00050 if ((hdr = gd->gd_free.gl_list)) { 00051 if (hdr->eg_next) /* clip it out of the list... */ 00052 hdr->eg_next->eg_prev = hdr->eg_prev; 00053 ev_assert(!hdr->eg_prev); 00054 gd->gd_free.gl_list = hdr->eg_next; /* move the list up... */ 00055 gd->gd_free.gl_count--; /* and update the count */ 00056 } else { 00057 ev_assert(!gd->gd_free.gl_count); 00058 if (!(hdr = (ev_genhdr_t *)malloc(gd->gd_size))) /* allocate some memory */ 00059 ev_return(ENOMEM); 00060 } 00061 00062 /* OK, now we have a genhdr structure. Let's initialize it... */ 00063 ev_gen_init(ctx, hdr, gd); 00064 00065 /* now link it into the active list... */ 00066 hdr->eg_next = gd->gd_active.gl_list; 00067 if (hdr->eg_next) 00068 hdr->eg_next->eg_prev = hdr; 00069 gd->gd_active.gl_list = hdr; 00070 00071 gd->gd_active.gl_count++; /* and increment the active list count */ 00072 00073 *((void **)gen) = hdr; /* return the initialized structure... */ 00074 00075 ev_return(0); /* all done! */ 00076 }