/* * * This file is part of bufferpool * * Copyright (C) 2007 by LScube team * See AUTHORS for more details * * bufferpool is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * bufferpool is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with bufferpool; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ #include #define BPNEWBUFF_RET_ERR \ do { \ free(slots); \ free(buffer); \ free(control); \ return NULL; \ } while (0) BPBuffer *bp_new(uint32_t buffer_size) { BPSlot *slots = NULL; BPBuffer *buffer = NULL; BPControl *control = NULL; uint32_t index; pthread_mutexattr_t mutex_attr; if (!buffer_size) return NULL; if (!(slots = (BPSlot *) calloc(buffer_size, sizeof(BPSlot)))) BPNEWBUFF_RET_ERR; // *** slots initialization for (index = 0; index < buffer_size - 1; index++) (slots[index]).next = index + 1; (slots[index]).next = 0; /*end of the list back to the head */ // control struct allocation if (!(control = malloc(sizeof(BPControl)))) BPNEWBUFF_RET_ERR; control->write_pos = buffer_size - 1; control->refs = 0; control->nslots = buffer_size; if (pthread_mutexattr_init(&mutex_attr)) BPNEWBUFF_RET_ERR; if (pthread_mutex_init(&control->syn, &mutex_attr)) BPNEWBUFF_RET_ERR; if (!(buffer = (BPBuffer *) malloc(sizeof(BPBuffer)))) BPNEWBUFF_RET_ERR; buffer->type = buff_local; *buffer->filename = '\0'; buffer->known_slots = buffer_size; // link all allocated structs buffer->slots = slots; buffer->control = control; return buffer; } #undef BPNEWBUFF_RET_ERR