/* grows internal buffer to satisfy required minimal capacity */ static void acl_array_grow(ACL_ARRAY *a, int min_capacity) { const int min_delta = 16; int delta; /* don't need to grow the capacity of the array */ if(a->capacity >= min_capacity) return; delta = min_capacity; /* make delta a multiple of min_delta */ delta += min_delta - 1; delta /= min_delta; delta *= min_delta; /* actual grow */ if (delta <= 0) return; a->capacity += delta; if (a->items) { a->items = (void **) acl_myrealloc(a->items, a->capacity * sizeof(void *)); } else { a->items = (void **) acl_mymalloc(a->capacity * sizeof(void *)); } /* reset, just in case */ memset(a->items + a->count, 0, (a->capacity - a->count) * sizeof(void *)); }
char *acl_url_encode(const char *str) { const char *myname = "acl_url_encode"; register int i, j, len, tmp_len; unsigned char *tmp; len = strlen(str); tmp_len = len; tmp = (unsigned char*) acl_mymalloc(len+1); if (tmp == NULL) acl_msg_fatal("%s(%d): malloc error", myname, __LINE__); for (i = 0, j = 0; i < len; i++, j++) { tmp[j] = (unsigned char)str[i]; if (tmp[j] == ' ') tmp[j] = '+'; else if (!isalnum(tmp[j]) && strchr("_-.", tmp[j]) == NULL) { tmp_len += 3; tmp = acl_myrealloc(tmp, tmp_len); if (!tmp) acl_msg_fatal("%s(%d): realloc error", myname, __LINE__); tmp[j++] = '%'; tmp[j++] = hex_enc_table[(unsigned char)str[i] >> 4]; tmp[j] = hex_enc_table[(unsigned char)str[i] & 0x0F]; } }
char *acl_url_encode(const char *str, ACL_DBUF_POOL *dbuf) { int i, j, len, tmp_len; unsigned char *tmp; len = (int) strlen(str); tmp_len = len; if (dbuf != NULL) tmp = (unsigned char*) acl_dbuf_pool_alloc(dbuf, len + 1); else tmp = (unsigned char*) acl_mymalloc(len + 1); for (i = 0, j = 0; i < len; i++, j++) { tmp[j] = (unsigned char) str[i]; if (tmp[j] == ' ') tmp[j] = '+'; else if (!isalnum(tmp[j]) && strchr("_-.", tmp[j]) == NULL) { tmp_len += 3; if (dbuf != NULL) { unsigned char *t = (unsigned char*) acl_dbuf_pool_alloc(dbuf, tmp_len); if (j > 0) memcpy(t, tmp, j); tmp = t; } else tmp = acl_myrealloc(tmp, tmp_len); tmp[j++] = '%'; tmp[j++] = enc_tab[(unsigned char)str[i] >> 4]; tmp[j] = enc_tab[(unsigned char)str[i] & 0x0F]; } }
void redis_command::argv_space(size_t n) { if (argv_size_ >= n) return; argv_size_ = n; if (argv_ == NULL) { argv_ = (const char**) acl_mymalloc(n * sizeof(char*)); argv_lens_ = (size_t*) acl_mymalloc(n * sizeof(size_t)); } else { argv_ = (const char**) acl_myrealloc(argv_, n * sizeof(char*)); argv_lens_ = (size_t*) acl_myrealloc(argv_lens_, n * sizeof(size_t)); } }
static void array_add(FIBER_ALT_ARRAY *a, FIBER_ALT *alt) { if (a->n == a->m) { a->m += 16; a->a = acl_myrealloc(a->a, a->m * sizeof(a->a[0])); } a->a[a->n++] = alt; }
void redis_request::reserve(size_t size) { if (size_ >= size) return; size_t len = size * sizeof(struct iovec); if (iov_ == NULL) iov_ = (struct iovec*) acl_mymalloc(len); else iov_ = (struct iovec*) acl_myrealloc(iov_, len); size_ = size; }
VBUF* session::vbuf_set(VBUF* buf, const void* str, size_t len, todo_t todo) { if (buf == NULL) { buf = (VBUF*) acl_mymalloc(sizeof(VBUF) + len + 1); buf->size = len + 1; } else if (buf->size <= len) { buf = (VBUF*) acl_myrealloc(buf, sizeof(VBUF) + len + 1); buf->size = len + 1; } buf->todo = todo; memcpy(buf->buf, str, len); buf->buf[len] = 0; buf->len = len; return buf; }
static void vstring_extend(ACL_VBUF *bp, int incr) { const char *myname = "vstring_extend"; unsigned used = bp->ptr - bp->data; int new_len; ACL_VSTRING *vp = (ACL_VSTRING *) bp->ctx; if (vp->maxlen > 0 && (int) ACL_VSTRING_LEN(vp) > vp->maxlen) acl_msg_warn("%s(%d), %s: reached the maxlen: %d", __FILE__, __LINE__, myname, vp->maxlen); /* * Note: vp->vbuf.len is the current buffer size (both on entry and on * exit of this routine). We round up the increment size to the buffer * size to avoid silly little buffer increments. With really large * strings we might want to abandon the length doubling strategy, and go * to fixed increments. */ /* new_len = bp->len + (bp->len > incr ? bp->len : incr); */ /* below come from redis-server/sds.c/sdsMakeRoomFor, which can * avoid memory double growing too large --- 2015.2.2, zsx */ new_len = bp->len + incr; if (new_len < MAX_PREALLOC) new_len *= 2; else new_len += MAX_PREALLOC; if (vp->slice) bp->data = (unsigned char *) acl_slice_pool_realloc( __FILE__, __LINE__, vp->slice, bp->data, new_len); else if (vp->dbuf) { const unsigned char *data = bp->data; bp->data = (unsigned char *) acl_dbuf_pool_alloc( vp->dbuf, new_len); memcpy(bp->data, data, used); } else bp->data = (unsigned char *) acl_myrealloc(bp->data, new_len); bp->len = new_len; bp->ptr = bp->data + used; bp->cnt = bp->len - used; }
ACL_FIBER *acl_fiber_create(void (*fn)(ACL_FIBER *, void *), void *arg, size_t size) { ACL_FIBER *fiber = fiber_alloc(fn, arg, size); __thread_fiber->count++; if (__thread_fiber->slot >= __thread_fiber->size) { __thread_fiber->size += 128; __thread_fiber->fibers = (ACL_FIBER **) acl_myrealloc( __thread_fiber->fibers, __thread_fiber->size * sizeof(ACL_FIBER *)); } fiber->slot = __thread_fiber->slot; __thread_fiber->fibers[__thread_fiber->slot++] = fiber; acl_fiber_ready(fiber); return fiber; }
int acl_fiber_set_specific(void *ctx, void (*free_fn)(void *)) { FIBER_LOCAL *local; ACL_FIBER *curr; int key; if (__thread_fiber == NULL || __thread_fiber->running == NULL) return -1; curr = __thread_fiber->running; key = curr->nlocal; local = (FIBER_LOCAL *) acl_mymalloc(sizeof(FIBER_LOCAL)); local->ctx = ctx; local->free_fn = free_fn; if (curr->nlocal % 64 == 0) curr->locals = (FIBER_LOCAL **) acl_myrealloc(curr->locals, (curr->nlocal + 64) * sizeof(FIBER_LOCAL*)); curr->locals[curr->nlocal++] = local; return key; }
static int vstring_extend(ACL_VBUF *bp, ssize_t incr) { const char *myname = "vstring_extend"; ssize_t used = (ssize_t) (bp->ptr - bp->data), new_len; ACL_VSTRING *vp = (ACL_VSTRING *) bp; if (vp->maxlen > 0 && (ssize_t) ACL_VSTRING_LEN(vp) >= vp->maxlen) { ACL_VSTRING_AT_OFFSET(vp, vp->maxlen - 1); ACL_VSTRING_TERMINATE(vp); acl_msg_warn("%s(%d), %s: overflow maxlen: %ld, %ld", __FILE__, __LINE__, myname, (long) vp->maxlen, (long) ACL_VSTRING_LEN(vp)); bp->flags |= ACL_VBUF_FLAG_EOF; return ACL_VBUF_EOF; } #ifdef ACL_WINDOWS if (bp->fd == ACL_FILE_INVALID && (bp->flags & ACL_VBUF_FLAG_FIXED)) #else if (bp->fd < 0 && (bp->flags & ACL_VBUF_FLAG_FIXED)) #endif { acl_msg_warn("%s(%d), %s: can't extend fixed buffer", __FILE__, __LINE__, myname); return ACL_VBUF_EOF; } /* * Note: vp->vbuf.len is the current buffer size (both on entry and on * exit of this routine). We round up the increment size to the buffer * size to avoid silly little buffer increments. With really large * strings we might want to abandon the length doubling strategy, and * go to fixed increments. */ #ifdef INCR_NO_DOUBLE /* below come from redis-server/sds.c/sdsMakeRoomFor, which can * avoid memory double growing too large --- 2015.2.2, zsx */ new_len = bp->len + incr; if (new_len < MAX_PREALLOC) new_len *= 2; else new_len += MAX_PREALLOC; #else new_len = bp->len + (bp->len > incr ? bp->len : incr); #endif if (vp->maxlen > 0 && new_len > vp->maxlen) new_len = vp->maxlen; if (vp->slice) bp->data = (unsigned char *) acl_slice_pool_realloc( __FILE__, __LINE__, vp->slice, bp->data, new_len); else if (vp->dbuf) { const unsigned char *data = bp->data; bp->data = (unsigned char *) acl_dbuf_pool_alloc( vp->dbuf, new_len); memcpy(bp->data, data, used); acl_dbuf_pool_free(vp->dbuf, data); } else if (bp->fd != ACL_FILE_INVALID) { #ifdef ACL_UNIX acl_off_t off = new_len - 1; if (acl_lseek(bp->fd, off, SEEK_SET) != (acl_off_t) off) acl_msg_fatal("lseek failed: %s, off: %lld", acl_last_serror(), off); if (acl_file_write(bp->fd, "\0", 1, 0, NULL, NULL) == ACL_VSTREAM_EOF) { acl_msg_fatal("write error: %s", acl_last_serror()); } #endif } else bp->data = (unsigned char *) acl_myrealloc(bp->data, new_len); bp->len = new_len; bp->ptr = bp->data + used; bp->cnt = bp->len - used; return 0; }
static ACL_FIBER *fiber_alloc(void (*fn)(ACL_FIBER *, void *), void *arg, size_t size) { ACL_FIBER *fiber; sigset_t zero; union cc_arg carg; ACL_RING *head; fiber_check(); #define APPL ACL_RING_TO_APPL /* try to reuse the fiber memory in dead queue */ head = acl_ring_pop_head(&__thread_fiber->dead); if (head == NULL) { fiber = (ACL_FIBER *) acl_mycalloc(1, sizeof(ACL_FIBER)); fiber->buff = (char *) acl_mymalloc(size); } else if ((fiber = APPL(head, ACL_FIBER, me))->size < size) fiber->buff = (char *) acl_myrealloc(fiber->buff, size); else size = fiber->size; fiber->errnum = 0; fiber->fn = fn; fiber->arg = arg; fiber->size = size; fiber->id = ++__thread_fiber->idgen; fiber->flag = 0; fiber->status = FIBER_STATUS_READY; carg.p = fiber; if (fiber->context == NULL) fiber->context = (ucontext_t *) acl_mymalloc(sizeof(ucontext_t)); sigemptyset(&zero); sigprocmask(SIG_BLOCK, &zero, &fiber->context->uc_sigmask); if (getcontext(fiber->context) < 0) acl_msg_fatal("%s(%d), %s: getcontext error: %s", __FILE__, __LINE__, __FUNCTION__, acl_last_serror()); fiber->context->uc_stack.ss_sp = fiber->buff + 8; fiber->context->uc_stack.ss_size = fiber->size - 64; #ifdef USE_JMP fiber->context->uc_link = NULL; #else fiber->context->uc_link = __thread_fiber->original.context; #endif #ifdef USE_VALGRIND /* avoding the valgrind's warning */ fiber->vid = VALGRIND_STACK_REGISTER(fiber->context->uc_stack.ss_sp, fiber->context->uc_stack.ss_sp + fiber->context->uc_stack.ss_size); #endif makecontext(fiber->context, (void(*)(void)) fiber_start, 2, carg.i[0], carg.i[1]); return fiber; }
ACL_IFCONF *acl_get_ifaddrs() { const char *myname = "acl_get_ifaddrs"; IP_ADAPTER_INFO info_temp, *infos, *info; ACL_IFCONF *ifconf; ULONG len = 0; int j; if (GetAdaptersInfo(&info_temp, &len) != ERROR_BUFFER_OVERFLOW) { acl_msg_error("%s(%d): GetAdaptersInfo eror(%s)", myname, __LINE__, acl_last_serror()); return (NULL); } infos = (IP_ADAPTER_INFO *) acl_mymalloc(len); if (GetAdaptersInfo(infos, &len) != NO_ERROR) { acl_msg_error("%s(%d): GetAdaptersInfo eror(%s)", myname, __LINE__, acl_last_serror()); acl_myfree(infos); return (NULL); } ifconf = (ACL_IFCONF*) acl_mymalloc(sizeof(ACL_IFCONF)); ifconf->length = len / sizeof(IP_ADAPTER_INFO) + 1; ifconf->addrs = (ACL_IFADDR*) acl_mycalloc(ifconf->length, sizeof(ACL_IFADDR)); for (info = infos, j = 0; info != NULL; info = info->Next) { if (info->Type == MIB_IF_TYPE_LOOPBACK) continue; if (strcmp(info->IpAddressList.IpAddress.String, "0.0.0.0") == 0) continue; if (acl_is_ip(info->IpAddressList.IpAddress.String) < 0) continue; ifconf->addrs[j].name = acl_mystrdup(info->AdapterName); ifconf->addrs[j].desc = acl_mystrdup(info->Description); snprintf(ifconf->addrs[j].ip, sizeof(ifconf->addrs[j].ip), "%s", info->IpAddressList.IpAddress.String); ifconf->addrs[j].addr = inet_addr(ifconf->addrs[j].ip); j++; if (j == ifconf->length) { ifconf->length *= 2; ifconf->addrs = (ACL_IFADDR*) acl_myrealloc(ifconf->addrs, ifconf->length * sizeof(ACL_IFADDR)); } } acl_myfree(infos); if (j == 0) { acl_myfree(ifconf->addrs); acl_myfree(ifconf); return (NULL); } ifconf->length = j; /* reset the ifconf->length */ /* set the iterator callback */ ifconf->iter_head = ifaddrs_iter_head; ifconf->iter_next = ifaddrs_iter_next; ifconf->iter_tail = ifaddrs_iter_tail; ifconf->iter_prev = ifaddrs_iter_prev; return (ifconf); }
ACL_IFCONF *acl_get_ifaddrs() { const char *myname = "acl_get_ifaddrs"; IP_ADAPTER_INFO info_temp, *infos, *info; ACL_IFCONF *ifconf; ULONG len = 0; int j; HMODULE hInst; PGAINFO *pGAInfo; hInst = LoadLibrary("iphlpapi.dll"); if(!hInst) { MessageBox(0, "iphlpapi.dll not supported in this platform!", "Error", 0); return (NULL); } pGAInfo = (PGAINFO*) GetProcAddress(hInst,"GetAdaptersInfo"); if (pGAInfo == NULL) { MessageBox(0, "can't find GetAdaptersInfo function!", "Error", 0); return (NULL); } if (pGAInfo(&info_temp, &len) != ERROR_BUFFER_OVERFLOW) { acl_msg_error("%s(%d): GetAdaptersInfo eror(%s)", myname, __LINE__, acl_last_serror()); FreeLibrary(hInst); return (NULL); } infos = (IP_ADAPTER_INFO *) acl_mymalloc(len); if (pGAInfo(infos, &len) != NO_ERROR) { acl_msg_error("%s(%d): GetAdaptersInfo eror(%s)", myname, __LINE__, acl_last_serror()); acl_myfree(infos); FreeLibrary(hInst); return (NULL); } ifconf = (ACL_IFCONF*) acl_mymalloc(sizeof(ACL_IFCONF)); ifconf->length = len / sizeof(IP_ADAPTER_INFO) + 1; ifconf->addrs = (ACL_IFADDR*) acl_mycalloc(ifconf->length, sizeof(ACL_IFADDR)); for (info = infos, j = 0; info != NULL; info = info->Next) { if (info->Type == MIB_IF_TYPE_LOOPBACK) continue; if (strcmp(info->IpAddressList.IpAddress.String, "0.0.0.0") == 0) continue; if (acl_is_ip(info->IpAddressList.IpAddress.String) < 0) continue; ifconf->addrs[j].name = acl_mystrdup(info->AdapterName); ifconf->addrs[j].desc = acl_mystrdup(info->Description); snprintf(ifconf->addrs[j].ip, sizeof(ifconf->addrs[j].ip), "%s", info->IpAddressList.IpAddress.String); ifconf->addrs[j].addr = inet_addr(ifconf->addrs[j].ip); j++; if (j == ifconf->length) { ifconf->length *= 2; ifconf->addrs = (ACL_IFADDR*) acl_myrealloc(ifconf->addrs, ifconf->length * sizeof(ACL_IFADDR)); } } acl_myfree(infos); if (j == 0) { acl_myfree(ifconf->addrs); acl_myfree(ifconf); FreeLibrary(hInst); return (NULL); } ifconf->length = j; /* reset the ifconf->length */ /* set the iterator callback */ ifconf->iter_head = ifaddrs_iter_head; ifconf->iter_next = ifaddrs_iter_next; ifconf->iter_tail = ifaddrs_iter_tail; ifconf->iter_prev = ifaddrs_iter_prev; FreeLibrary(hInst); return (ifconf); }