static void add_taint(Eterm mod_atom) { struct tainted_module_t* t; for (t=first_tainted_module ; t!=NULL; t=t->next) { if (t->module_atom == mod_atom) { return; } } t = erts_alloc_fnf(ERTS_ALC_T_TAINT, sizeof(*t)); if (t != NULL) { t->module_atom = mod_atom; t->next = first_tainted_module; first_tainted_module = t; } }
ErlDrvRWLock * erl_drv_rwlock_create(char *name) { #ifdef USE_THREADS ErlDrvRWLock *drwlck = erts_alloc_fnf(ERTS_ALC_T_DRV_RWLCK, (sizeof(ErlDrvRWLock) + (name ? sys_strlen(name) + 1 : 0))); if (drwlck) { if (ethr_rwmutex_init(&drwlck->rwmtx) != 0) { erts_free(ERTS_ALC_T_DRV_RWLCK, (void *) drwlck); drwlck = NULL; } else if (!name) drwlck->name = no_name; else { drwlck->name = ((char *) drwlck) + sizeof(ErlDrvRWLock); sys_strcpy(drwlck->name, name); } } return drwlck; #else return (ErlDrvRWLock *) NULL; #endif }
ErlDrvCond * erl_drv_cond_create(char *name) { #ifdef USE_THREADS ErlDrvCond *dcnd = erts_alloc_fnf(ERTS_ALC_T_DRV_CND, (sizeof(ErlDrvCond) + (name ? sys_strlen(name) + 1 : 0))); if (dcnd) { if (ethr_cond_init(&dcnd->cnd) != 0) { erts_free(ERTS_ALC_T_DRV_CND, (void *) dcnd); dcnd = NULL; } else if (!name) dcnd->name = no_name; else { dcnd->name = ((char *) dcnd) + sizeof(ErlDrvCond); sys_strcpy(dcnd->name, name); } } return dcnd; #else return (ErlDrvCond *) NULL; #endif }
ErlDrvMutex * erl_drv_mutex_create(char *name) { #ifdef USE_THREADS ErlDrvMutex *dmtx = erts_alloc_fnf(ERTS_ALC_T_DRV_MTX, (sizeof(ErlDrvMutex) + (name ? sys_strlen(name) + 1 : 0))); if (dmtx) { if (ethr_mutex_init(&dmtx->mtx) != 0) { erts_free(ERTS_ALC_T_DRV_MTX, (void *) dmtx); dmtx = NULL; } else if (!name) dmtx->name = no_name; else { dmtx->name = ((char *) dmtx) + sizeof(ErlDrvMutex); sys_strcpy(dmtx->name, name); } } return dmtx; #else return (ErlDrvMutex *) NULL; #endif }
static void *ethr_ll_alloc(size_t size) { return erts_alloc_fnf(ERTS_ALC_T_ETHR_LL, (Uint) size); }
static void *ethr_std_alloc(size_t size) { return erts_alloc_fnf(ERTS_ALC_T_ETHR_STD, (Uint) size); }
void* enif_alloc(size_t size) { return erts_alloc_fnf(ERTS_ALC_T_NIF, (Uint) size); }
voidpf erl_zlib_zalloc_callback (voidpf opaque, unsigned items, unsigned size) { (void) opaque; /* make compiler happy */ return erts_alloc_fnf(ERTS_ALC_T_ZLIB, items * size); }
int erl_drv_tsd_key_create(char *name, ErlDrvTSDKey *key) { char *name_copy; Uint old_used_tsd_keys_len; ErlDrvTSDKey res; if (!key) fatal_error(EINVAL, "erl_drv_tsd_key_create()"); if (!name) name_copy = no_name; else { name_copy = erts_alloc_fnf(ERTS_ALC_T_DRV_TSD, sizeof(char)*(strlen(name) + 1)); if (!name_copy) { *key = -1; return ENOMEM; } sys_strcpy(name_copy, name); } erts_mtx_lock(&tsd_mtx); *key = next_tsd_key; if (next_tsd_key < 0) res = ENOMEM; else { res = 0; ASSERT(!used_tsd_keys[next_tsd_key]); used_tsd_keys[next_tsd_key] = name_copy; if (max_used_tsd_key < next_tsd_key) max_used_tsd_key = next_tsd_key; if (max_used_tsd_key + 1 >= used_tsd_keys_len) { int i; old_used_tsd_keys_len = used_tsd_keys_len; if (used_tsd_keys_len + ERL_DRV_TSD_KEYS_INC >= INT_MAX) next_tsd_key = -1; else { char **new_used_tsd_keys; used_tsd_keys_len += ERL_DRV_TSD_KEYS_INC; new_used_tsd_keys = erts_realloc_fnf(ERTS_ALC_T_DRV_TSD, used_tsd_keys, (sizeof(char *) * used_tsd_keys_len)); if (!new_used_tsd_keys) next_tsd_key = -1; else { used_tsd_keys = new_used_tsd_keys; for (i = old_used_tsd_keys_len; i < used_tsd_keys_len; i++) used_tsd_keys[i] = NULL; } } } if (next_tsd_key >= 0) { do { next_tsd_key++; } while (used_tsd_keys[next_tsd_key]); } ASSERT(next_tsd_key < used_tsd_keys_len); } erts_mtx_unlock(&tsd_mtx); return res; }
/* expand queue to hold n elements in tail or head */ static int expandq(ErtsIOQueue* q, int n, int tail) /* tail: 0 if make room in head, make room in tail otherwise */ { int h_sz; /* room before header */ int t_sz; /* room after tail */ int q_sz; /* occupied */ int nvsz; SysIOVec* niov; ErtsIOQBinary** nbinv; h_sz = q->v_head - q->v_start; t_sz = q->v_end - q->v_tail; q_sz = q->v_tail - q->v_head; if (tail && (n <= t_sz)) /* do we need to expand tail? */ return 0; else if (!tail && (n <= h_sz)) /* do we need to expand head? */ return 0; else if (n > (h_sz + t_sz)) { /* need to allocate */ /* we may get little extra but it ok */ nvsz = (q->v_end - q->v_start) + n; niov = erts_alloc_fnf(q->alct, nvsz * sizeof(SysIOVec)); if (!niov) return -1; nbinv = erts_alloc_fnf(q->alct, nvsz * sizeof(ErtsIOQBinary**)); if (!nbinv) { erts_free(q->alct, (void *) niov); return -1; } if (tail) { sys_memcpy(niov, q->v_head, q_sz*sizeof(SysIOVec)); if (q->v_start != q->v_small) erts_free(q->alct, (void *) q->v_start); q->v_start = niov; q->v_end = niov + nvsz; q->v_head = q->v_start; q->v_tail = q->v_head + q_sz; sys_memcpy(nbinv, q->b_head, q_sz*sizeof(ErtsIOQBinary*)); if (q->b_start != q->b_small) erts_free(q->alct, (void *) q->b_start); q->b_start = nbinv; q->b_end = nbinv + nvsz; q->b_head = q->b_start; q->b_tail = q->b_head + q_sz; } else { sys_memcpy(niov+nvsz-q_sz, q->v_head, q_sz*sizeof(SysIOVec)); if (q->v_start != q->v_small) erts_free(q->alct, (void *) q->v_start); q->v_start = niov; q->v_end = niov + nvsz; q->v_tail = q->v_end; q->v_head = q->v_tail - q_sz; sys_memcpy(nbinv+nvsz-q_sz, q->b_head, q_sz*sizeof(ErtsIOQBinary*)); if (q->b_start != q->b_small) erts_free(q->alct, (void *) q->b_start); q->b_start = nbinv; q->b_end = nbinv + nvsz; q->b_tail = q->b_end; q->b_head = q->b_tail - q_sz; } } else if (tail) { /* move to beginning to make room in tail */ sys_memmove(q->v_start, q->v_head, q_sz*sizeof(SysIOVec)); q->v_head = q->v_start; q->v_tail = q->v_head + q_sz; sys_memmove(q->b_start, q->b_head, q_sz*sizeof(ErtsIOQBinary*)); q->b_head = q->b_start; q->b_tail = q->b_head + q_sz; } else { /* move to end to make room */ sys_memmove(q->v_end-q_sz, q->v_head, q_sz*sizeof(SysIOVec)); q->v_tail = q->v_end; q->v_head = q->v_tail-q_sz; sys_memmove(q->b_end-q_sz, q->b_head, q_sz*sizeof(ErtsIOQBinary*)); q->b_tail = q->b_end; q->b_head = q->b_tail-q_sz; } return 0; }