/* ** Setup a new buffer. Never fails. */ FDBUF * fd_create(int fd, int flags) { FDBUF *fdp; A_NEW(fdp); pthread_mutex_init(&fdp->refcnt_lock, NULL); fdp->refcnt = 1; fdp->flags = flags; fdp->fd = fd; pthread_mutex_init(&fdp->in_lock, NULL); fdp->ungetc = -1; fdp->in_start = 0; fdp->in_end = 0; fdp->inbufsize = FDBUF_INBUFSIZE; fdp->inbuf = a_malloc(fdp->inbufsize, "FDBUF inbuf"); pthread_mutex_init(&fdp->out_lock, NULL); fdp->lastc = -1; fdp->outbuflen = 0; fdp->outbufsize = FDBUF_OUTBUFSIZE; fdp->outbuf = a_malloc(fdp->outbufsize, "FDBUF outbuf"); return fdp; }
alex_al* relloc_al(alex_al* al) { if(al==NULL) return NULL; if(al->al_v==NULL) { al->al_v = (r_value*)a_malloc(sizeof(r_value)*DEF_AL_SIZE); memset(al->al_v, 0, sizeof(r_value)*DEF_AL_SIZE); al->al_size = DEF_AL_SIZE; al->al_len=0; } else { if(al->al_size <= al->al_len) { int size = sizeof(r_value)*(al->al_size+DEF_AL_SIZE); r_value* n_al_v = (r_value*)a_malloc(size); memset(n_al_v, 0, size); memcpy(n_al_v, al->al_v, sizeof(r_value)*al->al_size); a_free(al->al_v); al->al_v = n_al_v; al->al_size +=DEF_AL_SIZE; } } return al; }
VSTRING *vstring_new(u8 *str, u32 min_size) { VSTRING *vs_new; vs_new = (VSTRING *)a_malloc(sizeof(VSTRING)); vs_new->min = min_size; if (vs_new->min <= 0) vs_new->min = VSTRING_DEF_MIN; if ( (str != 0) && ((strlen(str)+1) > vs_new->min) ) vs_new->size = strlen(str)+1; // +1 for null character at end of string else vs_new->size = vs_new->min; vs_new->data = (u8 *)a_malloc(vs_new->size); memset(vs_new->data, 0, vs_new->size); if (str != 0) { strcpy(vs_new->data, str); vs_new->data[strlen(str)] = 0; } return vs_new; }
bool APerlinNoise2D::Init(int nBufferWidth, int nBufferHeight, float vAmplitude, int nWaveLength, float vPersistence, int nOctaveNum, ADWORD dwRandSeed) { // First try to release old resource; Release(); int i, x, y, k; m_dwSeed = dwRandSeed; // generate loop back smoothed random buffer if( nBufferWidth <= 0 || nBufferHeight <= 0 ) return false; m_nBufferWidth = nBufferWidth; m_nBufferHeight = nBufferHeight; float * pValues = (float *) a_malloc(sizeof(float) * m_nBufferWidth * m_nBufferHeight); if( NULL == pValues ) return false; m_pValues = (NOISEVALUE *) a_malloc(sizeof(NOISEVALUE) * m_nBufferWidth * m_nBufferHeight); if( NULL == m_pValues ) return false; for(k=0; k<3; k++) { // First create random number buffer; for(i=0; i<m_nBufferWidth * m_nBufferHeight; i++) pValues[i] = RandFloat(); // Now smooth the random number buffer; for(x=0; x<m_nBufferWidth; x++) { for(y=0; y<m_nBufferHeight; y++) { int n = y * m_nBufferWidth + x; int nLastX, nNextX, nLastY, nNextY; nLastX = x - 1; if( nLastX < 0 ) nLastX += m_nBufferWidth; nNextX = x + 1; if( nNextX >= m_nBufferWidth ) nNextX -= m_nBufferWidth; nLastY = y - 1; if( nLastY < 0 ) nLastY += m_nBufferHeight; nNextY = y + 1; if( nNextY >= m_nBufferHeight ) nNextY -= m_nBufferHeight; #define NOISEVALUEFUNC2D(x, y) (pValues[(y) * m_nBufferWidth + (x)]) m_pValues[n].v[k] = 1.0f / 16.0f * (NOISEVALUEFUNC2D(nLastX, nLastY) + NOISEVALUEFUNC2D(nLastX, nNextY) + NOISEVALUEFUNC2D(nNextX, nLastY) + NOISEVALUEFUNC2D(nNextX, nNextY)) + 1.0f / 8.0f * (NOISEVALUEFUNC2D(x, nLastY) + NOISEVALUEFUNC2D(x, nNextY) + NOISEVALUEFUNC2D(nLastX, y) + NOISEVALUEFUNC2D(nNextX, y)) + 1.0f / 4.0f * (NOISEVALUEFUNC2D(x, y)); } } } a_free(pValues); return InitParams(vAmplitude, nWaveLength, vPersistence, nOctaveNum); }
A ga(I t, I r, I n, I *s) { I k=WP(t,r,n); A z=a_malloc(k); AT(z)=t; AC(z)=1; AR(z)=r; AN(z)=n; if (r==1) { *AS(z)=n; } else if (r&&s) { ICPY(AS(z),s,r); } gcpush(z); R z; }
A gcinit(VO) { I k=WP(BOX,1,NOBJS); A memory; nmem=mtop=bytes=totbytes=0; memory=a_malloc(k); AT(memory)=BOX; AR(memory)=1; AN(memory)=*AS(memory)=NOBJS; objs=AAV(memory); R memory; }
bool AList::Init() { m_pHead = (ALISTELEMENT *)a_malloc(sizeof(ALISTELEMENT)); if( NULL == m_pHead ) return false; m_pTail = (ALISTELEMENT *)a_malloc(sizeof(ALISTELEMENT)); if( NULL == m_pTail ) return false; m_pHead->pData = m_pTail->pData = NULL; m_pHead->pLast = m_pTail->pNext = NULL; m_pHead->pNext = m_pTail; m_pTail->pLast = m_pHead; m_nSize = 0; return true; }
// 生成一个bnf tree node tree_node* new_tree_node(int line, bnf_type b_t) { tree_node* n_t_n = (tree_node*)a_malloc(sizeof(tree_node)); memset(n_t_n, 0, sizeof(tree_node)); n_t_n->b_t = b_t; n_t_n->line = line; return n_t_n; }
alex_al* _new_al(int def_count) { alex_al* ret_al = (alex_al*)a_malloc(sizeof(alex_al)); memset(ret_al, 0, sizeof(alex_al)); do { relloc_al(ret_al); }while(ret_al->al_size <= def_count); ret_al->al_len = (def_count <=0)?(0):(def_count); return ret_al; }
// initialise script u8 *script_new() { if ( (state.script_size > 0) && (script_head == 0) ) { script_head = a_malloc(state.script_size << 1); #warning set_mem_rm0() not implemented //set_mem_rm0(); } script_next = script_head; state.script_count = 0; return script_head; }
gc_node* gc_add(g_value g_v, e_gc_level gc_l) { gc_node* t_gc_node = (gc_node*)a_malloc(sizeof(gc_node)); memset(t_gc_node, 0, sizeof(gc_node)); t_gc_node->gc_value = g_v; t_gc_node->gc_level = gc_l; t_gc_node->next = alex_gc.gc_head; alex_gc.gc_head = t_gc_node; alex_gc.gc_size++; return t_gc_node; }
// does not bother to copy old string. void vstring_set_size(VSTRING *vs, u32 new_size) { if (vs != 0) { if (new_size < vs->min) new_size = vs->min; if (vs->size < new_size) { a_free(vs->data); vs->data = (u8 *)a_malloc(new_size); vs->size = new_size; //memset(vs->data, 0, new_size); } } }
void vstring_shrink(VSTRING *vs) { if ( (vs != 0) && (vs->data != 0) ) { u8 *new_data; vs->size = strlen(vs->data)+1; if (vs->size < vs->min) vs->size = vs->min; new_data = (u8 *)a_malloc(vs->size); memcpy(new_data, vs->data, vs->size); a_free(vs->data); vs->data = new_data; } }
static void *a_realloc(void *ptr, size_t size) { #ifdef ALIGNED_MALLOC return _aligned_realloc(ptr, size, ALIGNMENT); #elif ALIGNMENT_HACK long diff; if (!ptr) return a_malloc(size); diff = ((char *)ptr)[-1]; ptr = realloc((char*)ptr - diff, size + diff); if (ptr) ptr = (char *)ptr + diff; return ptr; #else return realloc(ptr, size); #endif }
bool AList::Insert(void* pDataToInsert, ALISTELEMENT * pElement, ALISTELEMENT ** ppElement) { ALISTELEMENT * pNewElement; if( NULL == pElement ) return false; pNewElement = (ALISTELEMENT *)a_malloc(sizeof(ALISTELEMENT)); if( NULL == pNewElement ) return false; pNewElement->pData = pDataToInsert; pElement->pLast->pNext = pNewElement; pNewElement->pLast = pElement->pLast; pNewElement->pNext = pElement; pElement->pLast = pNewElement; if( ppElement ) *ppElement = pNewElement; m_nSize ++; return true; }
gc_node* gc_add_str_table(char* str, e_gc_level gc_l) { unsigned int index =0; str_node* str_p = NULL; str_node* back_p = NULL; gc_node* gc_p = NULL; str_node* t_s_n = NULL; if(str==NULL) return NULL; index = gc_hash(str); str_p = alex_gc.gc_str_table.str_ptr[index]; back_p = str_p; while(str_p) { if(alex_strcmp(str_p->str, str)== 0) return str_p->gc_p; back_p = str_p; str_p = str_p->next; } t_s_n = (str_node*)a_malloc(sizeof(str_node)); memset(t_s_n, 0, sizeof(str_node)); gc_p = gc_add(gc_new_g_v_str(str), gc_l); t_s_n->str = gc_p->gc_value.sg_v.str; t_s_n->gc_p = gc_p; if(back_p== NULL) alex_gc.gc_str_table.str_ptr[index] = t_s_n; else back_p->next = t_s_n; return gc_p; }
void sdl_callback(void *userdata, u8 *stream, int len) { s16 chan_data[len / (int)sizeof(s16)]; int stream_len, stream_count; s16 *s_ptr, *c_ptr; SDL_CHAN *ch; int num_chan; int output = 0; #if WRITE_TO_DISK DATA *new_data; #endif (void) userdata; assert(stream); if (chan_list != NULL) { num_chan = list_length(chan_list); ch = list_element_head(chan_list); stream_len = len / (int)sizeof(s16); while (ch != NULL) { // get channel data(chan.userdata) if (ch->avail) { if (ch->callback( ch->userdata, (u8 *)&chan_data, len) == 0) { // divide by number of channels then add to stream stream_count = stream_len; s_ptr = (s16*)stream; c_ptr = (s16*)chan_data; output = 1; while(stream_count--) *(s_ptr++) += *(c_ptr++) / num_chan ; } else { ch->avail = 0; } } ch = node_next(ch); } } #if WRITE_TO_DISK new_data = list_add(list_data); new_data->data = (u8 *)a_malloc(len); memcpy(new_data->data, stream, len); new_data->len = len; #endif if (!output) { sndgen_kill_thread(); // call shutdown routin } }
ABYTE* af_AllocFileBuffer(AWORD size) { return (ABYTE*)a_malloc(size); }
int s_getgrnam_r(const char *name, struct group *grp, char *buffer, size_t bufsize, struct group **result) { #ifdef HAVE_GETPWNAM_R int code; memset(grp, 0, sizeof(*grp)); memset(buffer, 0, bufsize); #ifdef HAVE_UI_GETPW /* Unix International / Solaris / UnixWare */ while ((*result = getgrname_r(name, grp, buffer, bufsize)) == NULL && errno == EINTR) ; if (*result == NULL) code = errno; else code = 0; #elif HAVE_DCE_GETPW /* DCE/CMA */ while ((code = getgrnam_r(name, grp, buffer, bufsize)) != 0 && errno == EINTR) ; if (code == 0) *result = pwd; else code = errno; #else /* Posix version */ while ((code = getgrnam_r(name, grp, buffer, bufsize, result)) == EINTR) ; #endif return code; #else struct group *gp; int i, len; pthread_once(&grp_once, grp_lock_init); pthread_mutex_lock(&grp_lock); gp = getgrnam(name); if (gp == NULL) { pthread_mutex_unlock(&grp_lock); *result = NULL; return -1; } memset(grp, 0, sizeof(*grp)); grp->gr_name = strcopy(gp->gr_name, &buffer, &bufsize); grp->gr_passwd = strcopy(gp->gr_passwd, &buffer, &bufsize); grp->gr_gid = gp->gr_gid; for (i = 0; gp->gr_mem[i]; ++i) ; len = i; grp->gr_mem = a_malloc(sizeof(char *) * (len+1)); for (i = 0; i < len; ++i) grp->gr_mem[i] = strcopy(gp->gr_mem[i], &buffer, &bufsize); grp->gr_mem[i] = NULL; *result = grp; pthread_mutex_unlock(&grp_lock); return 0; #endif }
a_error_t udp_server(uint16_t port, udp_server_startup_callback startup, void *startup_arg, udp_server_callback func, void *arg, uint_fast8_t flags) { # if HAVE_WINSOCK2_H SOCKET sock; # else int sock; # endif uint8_t buf[MAX_UDP_SIZE]; void *tmp = (void *)buf; ssize_t buf_len; struct sockaddr_in local; struct sockaddr_in remote; # if HAVE_SOCKLEN_T socklen_t remote_len; # elif HAVE_WINSOCK2_H int remote_len; # else size_t remote_len; # endif # ifdef __WIN32__ if((sock = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) return a_flail_winsock_su(a_error_socket); # else if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return a_flail_posix_su(a_error_socket); # endif local.sin_family = AF_INET; local.sin_addr.s_addr = INADDR_ANY; local.sin_port = htons(port); # ifdef __WIN32__ if(bind(sock, (struct sockaddr *)(&local), sizeof(local)) == SOCKET_ERROR) return a_flail_winsock_su(a_error_bind); # else if(bind(sock, (struct sockaddr *)(&local), sizeof(local))) return a_flail_posix_su(a_error_bind); # endif /* ok, we're ready to receive, tell our callback */ if(startup && !startup(sock, startup_arg)) { /* call back told us to exit... */ # ifdef __WIN32__ if(!closesocket(sock)) return a_flail_winsock_su(a_error_close_socket); # else if(close(sock)) return a_flail_posix_su(a_error_close_socket); # endif } do { remote_len = sizeof(remote); if((buf_len = recvfrom(sock, (void *)(buf), MAX_UDP_SIZE, 0, (struct sockaddr *)(&remote), &remote_len)) < 0) # if __WIN32__ return a_flail_winsock_su(a_error_recvfrom); # else switch(errno) { case EINTR: /* interrupt */ case EAGAIN: /* try again */ continue; default: return a_flail_posix_su(a_error_recvfrom); } # endif if(flags & UDP_SERVER_FLAG_MALLOC) { if(!(tmp = a_malloc(sizeof(uint8_t) * (size_t)buf_len))) return a_error_last(); memcpy(tmp, buf, (size_t)buf_len); } } while(func(remote.sin_addr, ntohs(remote.sin_port), sock, tmp, (size_t)buf_len, arg) && a_udp_server_go); # ifdef __WIN32__ if(!(closesocket(sock))) return a_flail_winsock_su(a_error_close_socket); # else if(close(sock)) return a_flail_posix_su(a_error_close_socket); # endif return 0; }