void file_cache_push(FILE_CACHE *cache, const void *data, size_t size) { BUFFER *buffer = acl_mymalloc(sizeof(BUFFER)); buffer->buf = acl_mymalloc(size); buffer->size = size; memcpy(buffer->buf, data, size); buffer->ptr = buffer->buf; cache->size += size; acl_fifo_push(cache->fifo, buffer); }
static void thread_pool_tls(int nthread, int nalloc) { const char *myname = "thread_pool_tls"; acl_pthread_pool_t *thr_pool; void *ptr; time_t begin = time(NULL); int i; /* 创建线程池 */ thr_pool = acl_thread_pool_create(nthread, 0); for (i = 0; i < nalloc; i++) { /* 主线程分配内存 */ if (1) ptr = acl_mymalloc(64); else ptr = 0; /* 向线程池中添加任务, 将新内存传递给线程池处理 */ /* * ACL_METER_TIME("--31--"); */ acl_pthread_pool_add(thr_pool, run_thread, ptr); if (i % 100000 == 0) printf(">>>%s: i=%d\n", myname, i); /* * ACL_METER_TIME("--32--"); */ } /* 释放线程池 */ acl_pthread_pool_destroy(thr_pool); printf(">>>%s: time cost: %ld\n", myname, (long) (time(NULL) - begin)); }
char *acl_getenv(const char *name) { #ifdef ACL_WINDOWS const char *myname = "acl_getenv"; static acl_pthread_key_t buf_key = ACL_TLS_OUT_OF_INDEXES; char *buf; #define ENV_BUF_SIZE 4096 buf = (char*) acl_pthread_tls_get(&buf_key); if (buf == NULL) { if (buf_key == ACL_TLS_OUT_OF_INDEXES) { acl_msg_error("%s(%d): acl_pthread_tls_get error(%s)", myname, __LINE__, acl_last_serror()); return (NULL); } buf = (char*) acl_mymalloc(ENV_BUF_SIZE); acl_pthread_tls_set(buf_key, buf, (void (*)(void*)) acl_myfree_fn); } if (GetEnvironmentVariable(name, buf, ENV_BUF_SIZE) == 0) return (NULL); return (buf); #else return (getenv(name)); #endif }
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]; } }
virtual void run(HWND hWnd) { DB_IPC_DAT* data = (DB_IPC_DAT*) acl_mymalloc(sizeof(DB_IPC_DAT)); data->db = db_; data->query = query_; data->rows = NULL; data->affected_rows = 0; if (db_->open() == false) ::PostMessage(hWnd, DB_ERR_OPEN + WM_USER, 0, (LPARAM) data); else if (has_res_) { if (db_->sql_select(sql_.c_str()) == false) ::PostMessage(hWnd, DB_ERR_EXEC_SQL + WM_USER, 0, (LPARAM) data); { data->rows = db_->get_result(); ::PostMessage(hWnd, DB_OK + WM_USER, 0, (LPARAM) data); } } else if (db_->sql_update(sql_.c_str()) == false) ::PostMessage(hWnd, DB_ERR_EXEC_SQL + WM_USER, 0, (LPARAM) data); else { data->rows = db_->get_result(); // 修改操作,需要取一下 SQL 操作影响的行数 data->affected_rows = db_->affect_count(); ::PostMessage(hWnd, DB_OK + WM_USER, 0, (LPARAM) data); //::SendMessage(hWnd, DB_OK + WM_USER, 0, (LPARAM) data); } // 因为本请求对象是动态创建的,所以需要释放 delete this; }
ACL_MBOX *acl_mbox_create(void) { ACL_MBOX *mbox; ACL_SOCKET fds[2]; if (acl_sane_socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) { acl_msg_error("%s(%d), %s: acl_duplex_pipe error %s", __FILE__, __LINE__, __FUNCTION__, acl_last_serror()); return NULL; } mbox = (ACL_MBOX *) acl_mymalloc(sizeof(ACL_MBOX)); mbox->in = acl_vstream_fdopen(fds[0], O_RDONLY, sizeof(__key), 0, ACL_VSTREAM_TYPE_SOCK); mbox->out = acl_vstream_fdopen(fds[1], O_WRONLY, sizeof(__key), 0, ACL_VSTREAM_TYPE_SOCK); mbox->nsend = 0; mbox->nread = 0; mbox->ypipe = acl_ypipe_new(); mbox->lock = (acl_pthread_mutex_t *) acl_mycalloc(1, sizeof(acl_pthread_mutex_t)); if (acl_pthread_mutex_init(mbox->lock, NULL) != 0) acl_msg_fatal("%s(%d), %s: acl_pthread_mutex_init error", __FILE__, __LINE__, __FUNCTION__); return mbox; }
/* 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 *)); }
/* Create a semaphore */ ACL_SEM *acl_sem_create2(const char *pathname, unsigned int initial_value) { char myname[] = "acl_sem_create2"; ACL_SEM *sem; char buf[256]; /* Allocate sem memory */ sem = (ACL_SEM *) acl_mymalloc(sizeof(*sem)); if (sem == NULL) { acl_msg_error("%s, %s(%d): malloc error(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); return (NULL); } /* Create the semaphore, with max value 32K */ sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, pathname); sem->count = initial_value; if (!sem->id) { acl_msg_error("%s, %s(%d): Couldn't create semaphore(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); acl_myfree(sem); return (NULL); } return(sem); }
TLS_SCACHE *tls_scache_open(const char *dbname, const char *cache_label, int verbose, int timeout) { const char *myname = "tls_scache_open"; TLS_SCACHE *cp; DICT *dict; /* * Logging. */ if (verbose) acl_msg_info("open %s TLS cache %s", cache_label, dbname); /* * Open the dictionary with O_TRUNC, so that we never have to worry about * opening a damaged file after some process terminated abnormally. */ #ifdef SINGLE_UPDATER #define DICT_FLAGS (DICT_FLAG_DUP_REPLACE) #elif defined(ACL_UNIX) #define DICT_FLAGS \ (DICT_FLAG_DUP_REPLACE | DICT_FLAG_LOCK | DICT_FLAG_SYNC_UPDATE) #elif defined(WIN32) #define DICT_FLAGS \ (DICT_FLAG_DUP_REPLACE | DICT_FLAG_SYNC_UPDATE) #endif dict = dict_open(dbname, O_RDWR | O_CREAT | O_TRUNC, DICT_FLAGS); /* * Sanity checks. */ if (dict->lock_fd < 0) acl_msg_fatal("%s: dictionary %s is not a regular file", myname, dbname); #ifdef SINGLE_UPDATER if (acl_myflock(dict->lock_fd, INTERNAL_LOCK, MYFLOCK_OP_EXCLUSIVE | MYFLOCK_OP_NOWAIT) < 0) acl_msg_fatal("%s: cannot lock dictionary %s for exclusive use: %s", myname, dbname, acl_last_serror()); #endif if (dict->update == 0) acl_msg_fatal("%s: dictionary %s does not support update operations", myname, dbname); if (dict->delete_it == 0) acl_msg_fatal("%s: dictionary %s does not support delete operations", myname, dbname); if (dict->sequence == 0) acl_msg_fatal("%s: dictionary %s does not support sequence operations", myname, dbname); /* * Create the TLS_SCACHE object. */ cp = (TLS_SCACHE *) acl_mymalloc(sizeof(*cp)); cp->flags = 0; cp->db = dict; cp->cache_label = acl_mystrdup(cache_label); cp->verbose = verbose; cp->timeout = timeout; cp->saved_cursor = 0; return (cp); }
TLS_PRNG_SRC *tls_prng_egd_open(const char *name, int timeout) { const char *myname = "tls_prng_egd_open"; TLS_PRNG_SRC *egd; ACL_SOCKET fd; if (acl_msg_verbose) acl_msg_info("%s: connect to EGD server %s", myname, name); #ifdef ACL_UNIX fd = acl_unix_connect(name, ACL_BLOCKING, timeout); #elif defined(ACL_MS_WINDOWS) fd = acl_inet_connect(name, ACL_BLOCKING, timeout); #endif if (fd < 0) { if (acl_msg_verbose) acl_msg_info("%s: cannot connect to EGD server %s: %s", myname, name, acl_last_serror()); return (0); } else { egd = (TLS_PRNG_SRC *) acl_mymalloc(sizeof(*egd)); egd->fd.sock = fd; egd->name = acl_mystrdup(name); egd->timeout = timeout; if (acl_msg_verbose) acl_msg_info("%s: connected to EGD server %s", myname, name); return (egd); } }
query& query::set_date(const char* name, time_t value, const char* fmt /* = "%Y-%m-%d %H:%M:%S" */) { string key(name); key.lower(); del_param(key); string buf(128); if (to_date(value, buf, fmt) == NULL) { logger_error("to_date_time failed, time: %ld", (long) value); return *this; } size_t len = buf.length(); query_param* param = (query_param*) acl_mymalloc(sizeof(query_param) + len + 1); param->type = DB_PARAM_STR; memcpy(param->v.S, buf.c_str(), len); param->v.S[len] = 0; param->dlen = (int) len; params_[key] = param; return *this; }
ACL_WATCHDOG *acl_watchdog_create(unsigned timeout, ACL_WATCHDOG_FN action, char *context) { const char* myname = "acl_watchdog_create"; struct sigaction sig_action; ACL_WATCHDOG *wp; wp = (ACL_WATCHDOG *) acl_mymalloc(sizeof(*wp)); if ((wp->timeout = timeout / ACL_WATCHDOG_STEPS) == 0) acl_msg_panic("%s: timeout %d too small", myname, timeout); wp->action = action; wp->context = context; wp->saved_watchdog = acl_watchdog_curr; wp->saved_time = alarm(0); sigemptyset(&sig_action.sa_mask); #ifdef SA_RESTART sig_action.sa_flags = SA_RESTART; #else sig_action.sa_flags = 0; #endif sig_action.sa_handler = acl_watchdog_event; if (sigaction(SIGALRM, &sig_action, &wp->saved_action) < 0) acl_msg_fatal("%s: sigaction(SIGALRM): %s", myname, acl_last_serror()); if (acl_msg_verbose > 1) acl_msg_info("%s: %p %d", myname, (void *) wp, timeout); return (acl_watchdog_curr = wp); }
int acl_scan_dir_push(ACL_SCAN_DIR *scan, const char *path) { const char *myname = "acl_scan_dir_push"; ACL_SCAN_INFO *info; info = (ACL_SCAN_INFO *) acl_mymalloc(sizeof(*info)); if (scan->current) { info->path = acl_concatenate(ACL_SCAN_DIR_PATH(scan), "/", path, (char *) 0); } else { info->path = acl_mystrdup(path); } if ((info->dir_name = opendir(info->path)) == 0) { char tbuf[256]; acl_msg_error("%s(%d), %s: open directory(%s) error(%s)", __FILE__, __LINE__, myname, info->path, acl_last_strerror(tbuf, sizeof(tbuf))); return (-1); } if (acl_msg_verbose > 1) acl_msg_info("%s: open %s", myname, info->path); info->parent = scan->current; scan->current = info; return (0); }
void fiber_io_check(void) { if (__thread_fiber != NULL) return; acl_assert(acl_pthread_once(&__once_control, thread_init) == 0); __maxfd = acl_open_limit(0); if (__maxfd <= 0) __maxfd = MAXFD; __thread_fiber = (FIBER_TLS *) acl_mymalloc(sizeof(FIBER_TLS)); __thread_fiber->event = event_create(__maxfd); __thread_fiber->io_fibers = (ACL_FIBER **) acl_mycalloc(__maxfd, sizeof(ACL_FIBER *)); __thread_fiber->ev_fiber = acl_fiber_create(fiber_io_loop, __thread_fiber->event, STACK_SIZE); __thread_fiber->io_count = 0; __thread_fiber->nsleeping = 0; __thread_fiber->io_stop = 0; acl_ring_init(&__thread_fiber->ev_timer); if ((unsigned long) acl_pthread_self() == acl_main_thread_self()) { __main_fiber = __thread_fiber; atexit(fiber_io_main_free); } else if (acl_pthread_setspecific(__fiber_key, __thread_fiber) != 0) acl_msg_fatal("acl_pthread_setspecific error!"); }
void acl_yqueue_push(ACL_YQUEUE *self) { chunk_t *sc; self->pushs++; self->back_chunk = self->end_chunk; self->back_pos = self->end_pos; self->end_pos++; if (self->end_pos != CHUNK_SIZE) return; sc = (chunk_t *) acl_atomic_xchg(self->spare_chunk, NULL); if (sc) { self->end_chunk->next = sc; sc->prev = self->end_chunk; } else { self->end_chunk->next = (chunk_t *) acl_mymalloc(sizeof(chunk_t)); memset(self->end_chunk->next, 0, sizeof(chunk_t)); acl_assert(self->end_chunk); self->end_chunk->next->prev = self->end_chunk; } self->end_chunk = self->end_chunk->next; self->end_pos = 0; }
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]; } }
char *tls_fingerprint(X509 *peercert, const char *dgst) { const char *myname = "tls_fingerprint"; const EVP_MD *md_alg; unsigned char md_buf[EVP_MAX_MD_SIZE]; unsigned int md_len; int i; char *result = 0; /* Previously available in "init" routine. */ if ((md_alg = EVP_get_digestbyname(dgst)) == 0) acl_msg_panic("%s: digest algorithm \"%s\" not found", myname, dgst); /* Fails when serialization to ASN.1 runs out of memory */ if (X509_digest(peercert, md_alg, md_buf, &md_len) == 0) acl_msg_fatal("%s: error computing certificate %s digest (out of memory?)", myname, dgst); /* Check for OpenSSL contract violation */ if (md_len > EVP_MAX_MD_SIZE || (int) md_len >= INT_MAX / 3) acl_msg_panic("%s: unexpectedly large %s digest size: %u", myname, dgst, md_len); result = acl_mymalloc(md_len * 3); for (i = 0; i < (int) md_len; i++) { result[i * 3] = hexcodes[(md_buf[i] & 0xf0) >> 4U]; result[(i * 3) + 1] = hexcodes[(md_buf[i] & 0x0f)]; result[(i * 3) + 2] = (i + 1 != (int) md_len) ? ':' : '\0'; } return (result); }
int acl_unix_trigger(ACL_EVENT *event, const char *service, const char *buf, int len, int timeout) { const char *myname = "acl_unix_trigger"; struct ACL_UNIX_TRIGGER *up; ACL_SOCKET fd; if (acl_msg_verbose > 0) acl_msg_info("%s: service %s", myname, service); /* * Connect... */ if ((fd = acl_unix_connect(service, ACL_BLOCKING, timeout)) < 0) { if (acl_msg_verbose) acl_msg_warn("%s: connect to %s: %s", myname, service, strerror(errno)); return -1; } acl_close_on_exec(fd, ACL_CLOSE_ON_EXEC); /* * Stash away context. */ up = (struct ACL_UNIX_TRIGGER *) acl_mymalloc(sizeof(*up)); up->service = acl_mystrdup(service); up->stream = acl_vstream_fdopen(fd, O_RDWR, 4096, timeout, ACL_VSTREAM_TYPE_LISTEN_UNIX); /* * Write the request... */ if (acl_vstream_writen(up->stream, buf, len) < 0 || acl_vstream_writen(up->stream, "", 1) < 0) { if (acl_msg_verbose) acl_msg_warn("%s: write to %s: %s", myname, service, strerror(errno)); } /* * Wakeup when the peer disconnects, or when we lose patience. */ #ifdef __USE_TIMER if (timeout > 0) acl_event_request_timer(event, acl_unix_trigger_timer, (void *) up, (timeout + 100) * 1000000); acl_event_enable_read(event, up->stream, 0, acl_unix_trigger_event, (void *) up); #else if (timeout > 0) acl_event_enable_read(event, up->stream, timeout + 100, acl_unix_trigger_event, (void *) up); else acl_event_enable_read(event, up->stream, 0, acl_unix_trigger_event, (void *) up); #endif return 0; }
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)); } }
ACL_YQUEUE *acl_yqueue_new(void) { ACL_YQUEUE *self; self = (ACL_YQUEUE *) acl_mymalloc(sizeof(ACL_YQUEUE)); memset(self, 0, sizeof(ACL_YQUEUE)); self->begin_chunk = (chunk_t *) acl_mymalloc(sizeof(chunk_t)); memset(self->begin_chunk, 0, sizeof(chunk_t)); self->begin_pos = 0; self->back_chunk = NULL; self->back_pos = 0; self->end_chunk = self->begin_chunk; self->end_pos = 0; self->spare_chunk = acl_atomic_new(); acl_atomic_set(self->spare_chunk, NULL); return self; }
const std::list<rfc822_addr*>& rfc822::parse_addrs(const char* in, const char* to_charset /* = "utf-8" */) { reset(); if (to_charset == NULL) to_charset = "gb18030"; if (in == NULL || *in == 0) { logger_error("input invalid"); return (addrs_); } TOK822 *tree = tok822_parse(in); if (tree == NULL) { logger_error("tok822_parse(%s) error", in); return (addrs_); } const ACL_VSTRING* comment_prev = NULL; string buf; for (TOK822 *tp = tree; tp; tp = tp->next) { if (tp->type == TOK822_ATOM || tp->type == TOK822_COMMENT || tp->type == TOK822_QSTRING || tp->vstr != NULL) { comment_prev = tp->vstr; } if (tp->type != TOK822_ADDR || tp->head == NULL) continue; ACL_VSTRING* addrp = acl_vstring_alloc(32); (void) tok822_internalize(addrp, tp->head, TOK822_STR_DEFL); rfc822_addr* addr = (rfc822_addr*) acl_mymalloc(sizeof(rfc822_addr)); addr->addr = acl_vstring_export(addrp); if (comment_prev) { buf.clear(); rfc2047::decode(STR(comment_prev), (int) LEN(comment_prev), &buf, to_charset); addr->comment = acl_mystrdup(buf.c_str()); comment_prev = NULL; } else addr->comment = NULL; addrs_.push_back(addr); } tok822_free_tree(tree); return (addrs_); }
static ZDB_IO_BLK *io_blk_new(ZDB_IO *io) { ZDB_IO_BLK *blk; if (io->blk_slice) blk = (ZDB_IO_BLK*) acl_slice_alloc(io->blk_slice); else blk = (ZDB_IO_BLK*) acl_mymalloc(sizeof(ZDB_IO_BLK)); if (io->dat_slice) blk->dat = (char*) acl_slice_alloc(io->dat_slice); else blk->dat = (char*) acl_mymalloc(io->blk_len); blk->flag = 0; blk->io = io; __n++; return (blk); }
CHttpReport::CHttpReport(int type, const char *data, int len) { Init(); m_pBuf = (char*) acl_mymalloc((size_t) len + 1); memcpy(m_pBuf, data, (size_t) len); m_pBuf[len] = 0; m_nBuf = len; m_type = type; }
void report(HWND hWnd, UINT nMsg) { HTTP_IPC_DAT* data = (HTTP_IPC_DAT*) acl_mymalloc(sizeof(HTTP_IPC_DAT)); memcpy(data, &data_, sizeof(HTTP_IPC_DAT)); data->i_hdr_res = hdr_res_; data->i_error = HTTP_OK; ::PostMessage(hWnd, nMsg + WM_USER, 0, (LPARAM) data); }
ACL_FIBER_MUTEX *acl_fiber_mutex_create(void) { ACL_FIBER_MUTEX *lk = (ACL_FIBER_MUTEX *) acl_mymalloc(sizeof(ACL_FIBER_MUTEX)); lk->owner = NULL; acl_ring_init(&lk->me); acl_ring_init(&lk->waiting); return lk; }
void master_service::proc_on_init() { if (var_cfg_buf_size <= 0) var_cfg_buf_size = 1024; res_buf_ = (char*) acl_mymalloc(var_cfg_buf_size + 1); int i; for (i = 0; i < var_cfg_buf_size; i++) res_buf_[i] = 'X'; res_buf_[i] = 0; }
ACL_ATOMIC *acl_atomic_new(void) { ACL_ATOMIC *self = (ACL_ATOMIC*) acl_mymalloc(sizeof(ACL_ATOMIC)); #ifndef HAS_ATOMIC acl_pthread_mutex_init(&self->lock, NULL); #endif self->value = NULL; return self; }
http_rpc::http_rpc(acl::aio_socket_stream* client, unsigned buf_size) : client_(client) , buf_size_(buf_size) { res_buf_ = (char*) acl_mymalloc(buf_size + 1); unsigned i; for (i = 0; i < buf_size; i++) res_buf_[i] = 'x'; res_buf_[i] = 0; }
ACL_FIBER_SEM *acl_fiber_sem_create(int num) { ACL_FIBER_SEM *sem = (ACL_FIBER_SEM *) acl_mymalloc(sizeof(ACL_FIBER_SEM)); sem->num = num; acl_ring_init(&sem->waiting); sem->tid = acl_pthread_self(); return sem; }
void report_error(HWND hWnd, http_status_t errnum) { HTTP_IPC_DAT* data = (HTTP_IPC_DAT*) acl_mymalloc(sizeof(HTTP_IPC_DAT)); memcpy(data, &data_, sizeof(HTTP_IPC_DAT)); data->i_hdr_res = hdr_res_; data->i_error = errnum; // 向主线程发送结果 ::PostMessage(hWnd, HTTP_MSG_ERR + WM_USER, 0, (LPARAM) data); }