/* linux version */ int gw_gethostbyname(struct hostent *ent, const char *name, char **buff) { struct hostent *tmphp, hp; int herr, res; size_t bufflen; tmphp = NULL; /* for compiler please */ bufflen = 1024; *buff = (char*) gw_malloc(bufflen); while ((res=gethostbyname_r(name, &hp,*buff, bufflen, &tmphp, &herr)) == ERANGE) { /* enlarge the buffer */ bufflen *= 2; *buff = (char*) gw_realloc(*buff, bufflen); } if (res != 0 || tmphp == NULL) { error(herr, "Error while gw_gethostbyname occurs."); gw_free(*buff); *buff = NULL; res = -1; } else { *ent = hp; } return res; }
int load_add_interval(Load *load, int interval) { int i; struct load_entry *entry; if (load == NULL) return -1; gw_rwlock_wrlock(load->lock); /* first look if we have equal interval added already */ for (i = 0; i < load->len; i++) { if (load->entries[i]->interval == interval) { gw_rwlock_unlock(load->lock); return -1; } } /* so no equal interval there, add new one */ entry = gw_malloc(sizeof(struct load_entry)); entry->prev = entry->curr = 0.0; entry->interval = interval; entry->dirty = 1; time(&entry->last); load->entries = gw_realloc(load->entries, sizeof(struct load*) * (load->len + 1)); load->entries[load->len] = entry; load->len++; gw_rwlock_unlock(load->lock); return 0; }
static void make_bigger(gw_prioqueue_t *queue, long items) { size_t size = queue->size; size_t new_size = sizeof(*queue->tab) * (queue->len + items); if (size >= new_size) return; queue->tab = gw_realloc(queue->tab, new_size); queue->size = new_size; }
/* * Add a timer to the heap. Do this by adding it at the end, then * moving it up or down as necessary to achieve partial ordering. */ static void heap_insert(TimerHeap *heap, Timer *timer) { heap->len++; if (heap->len > heap->size) { heap->tab = gw_realloc(heap->tab, heap->len * sizeof(heap->tab[0])); heap->size = heap->len; } heap->tab[heap->len - 1] = timer; timer->index = heap->len - 1; heap_adjust(heap, timer->index); }
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) { register int realsize = size * nmemb; struct MemoryStruct *mem = (struct MemoryStruct *) data; mem->memory = (char *) gw_realloc(mem->memory, mem->size + realsize + 1); if (mem->memory) { memcpy(&(mem->memory[mem->size]), ptr, realsize); mem->size += realsize; mem->memory[mem->size] = 0; } return realsize; }
/* solaris */ int gw_gethostbyname(struct hostent *ent, const char *name, char **buff) { int herr = 0; size_t bufflen = 1024; int res = 0; struct hostent *tmphp = NULL; *buff = gw_malloc(bufflen); while ((tmphp = gethostbyname_r(name, ent, *buff, bufflen, &herr)) == NULL && (errno == ERANGE)) { /* Enlarge the buffer. */ bufflen *= 2; *buff = (char *) gw_realloc(*buff, bufflen); } if (tmphp == NULL) { error(herr, "Error while gw_gethostbyname occurs."); gw_free(*buff); *buff = NULL; res = -1; } return res; }
int smscenter_read_into_buffer(SMSCenter *smsc) { char *p; int ret, result; fd_set read_fd; struct timeval tv, tvinit; size_t bytes_read; tvinit.tv_sec = 0; tvinit.tv_usec = 1000; bytes_read = 0; result = 0; for (;;) { FD_ZERO(&read_fd); FD_SET(smsc->socket, &read_fd); tv = tvinit; ret = select(smsc->socket + 1, &read_fd, NULL, NULL, &tv); if (ret == -1) { if (errno == EINTR) goto got_data; if (errno == EAGAIN) goto got_data; error(errno, "Error doing select for socket"); goto error; } else if (ret == 0) goto got_data; if (smsc->buflen == smsc->bufsize) { p = gw_realloc(smsc->buffer, smsc->bufsize * 2); smsc->buffer = p; smsc->bufsize *= 2; } ret = read(smsc->socket, smsc->buffer + smsc->buflen, 1); if (ret == -1) { error(errno, "Reading from `%s' port `%d' failed.", smsc->hostname, smsc->port); goto error; } if (ret == 0) goto eof; smsc->buflen += ret; bytes_read += ret; if (bytes_read >= MAX_READ_INTO_BUFFER) break; } eof: ret = 0; goto unblock; got_data: ret = 1; goto unblock; error: ret = -1; goto unblock; unblock: return ret; }
/* * Realloc callback function to get tracking of OCI allocs. */ static void *oracle_realloc(void *ctx, void *ptr, size_t size) { void *ret = gw_realloc(ptr, size); debug("dbpool.oracle",0,"oracle_realloc called size=%ld", (long) size); return ret; }
/* * Make the array bigger. It might be more efficient to make the size * bigger than what is explicitly requested. * * Assume list has been locked for a single operation already. */ static void make_bigger(List *list, long items) { long old_size, new_size; long len_at_beginning, len_at_end; if (list->len + items <= list->tab_size) return; old_size = list->tab_size; new_size = old_size + items; list->tab = gw_realloc(list->tab, new_size * sizeof(void *)); list->tab_size = new_size; /* * Now, one of the following situations is in effect * (* is used, empty is unused element): * * Case 1: Used area did not wrap. No action is necessary. * * old_size new_size * v v * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | |*|*|*|*|*|*| | | | | | | | | | | | | | | | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * ^ ^ * start start+len * * Case 2: Used area wrapped, but the part at the beginning * of the array fits into the new area. Action: move part * from beginning to new area. * * old_size new_size * v v * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |*|*| | | | | | | |*|*|*| | | | | | | | | | | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * ^ ^ * start+len start * * Case 3: Used area wrapped, and the part at the beginning * of the array does not fit into the new area. Action: move * as much as will fit from beginning to new area and move * the rest to the beginning. * * old_size new_size * v v * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |*|*|*|*|*|*|*|*|*| | | | | | | | |*|*|*|*| | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * ^ ^ * start+len start */ gw_assert(list->start < old_size || (list->start == 0 && old_size == 0)); if (list->start + list->len > old_size) { len_at_end = old_size - list->start; len_at_beginning = list->len - len_at_end; if (len_at_beginning <= new_size - old_size) { /* This is Case 2. */ memmove(list->tab + old_size, list->tab, len_at_beginning * sizeof(void *)); } else { /* This is Case 3. */ memmove(list->tab + old_size, list->tab, (new_size - old_size) * sizeof(void *)); memmove(list->tab, list->tab + (new_size - old_size), (len_at_beginning - (new_size - old_size)) * sizeof(void *)); } } }