char *ft_strtrim_p(char const *s) { int i; int j; char *str; if (s == NULL) return (NULL); i = 0; while (*(s + i) && ft_isspace(*(s + i)) == 1) ++i; if (!*(s + i)) { if (!(str = (char *)gmalloc(sizeof(char) * (1)))) return (NULL); *(str + 0) = '\0'; return (str); } j = (int)ft_strlen(s) - 1; while (*(s + j) && ft_isspace(*(s + j)) == 1) --j; if (!(str = (char *)gmalloc(sizeof(char) * (j - i + 1 + 1)))) return (NULL); str = ft_strsub(s, i, j - i + 1); return (str); }
static char *ft_good_path(char **all, char **env) { int i; char *tmp; char *str; char **paths; struct stat *buf; if (!(paths = ft_find_paths_in_env(env))) return (NULL); if (!(buf = (struct stat *)gmalloc(sizeof(*buf)))) return (NULL); i = 0; while (paths[i]) { if (!(str = (char *)gmalloc(sizeof(*str) * ft_strlen(paths[i]) + 1))) return (NULL); tmp = ft_strjoin(paths[i], "/"); str = ft_strjoin(tmp, all[0]); gofree((void *)tmp); if (lstat(str, buf) == 0) return (str); ++i; } return (all[0]); }
int ft_init_disk2(t_obj *obj) { t_dis **ldis; int i; obj->nb[DIS] = 11; if (!(((t_dis ***)obj->type)[DIS] = (t_dis **)gmalloc(sizeof(t_dis *) * obj->nb[DIS]))) return (-1); ldis = ((t_dis ***)obj->type)[DIS]; i = -1; while (++i < obj->nb[DIS]) { if (!(ldis[i] = (t_dis *)gmalloc(sizeof(t_dis)))) return (-1); } ft_init_disk2_2(ldis); ft_init_disk2_3(ldis); ft_init_vect(&ldis[10]->c, -3, 0.5, -598); ldis[10]->r = 0.32; ft_init_vect(&ldis[10]->n, 0.2, 1, 0.3); ft_normalize_vect(&ldis[10]->n); ft_init_color(&ldis[10]->sf, 0, 20, 80); return (0); }
Lsym* enter(char *name, int t) { Lsym *s; ulong h; char *p; Value *v; h = 0; for(p = name; *p; p++) h = h*3 + *p; h %= Hashsize; s = gmalloc(sizeof(Lsym)); s->name = strdup(name); s->hash = hash[h]; hash[h] = s; s->lexval = t; v = gmalloc(sizeof(Value)); s->v = v; v->vstore.fmt = 'X'; v->type = TINT; return s; }
char *ft_strtrim(char const *s) { int i; int j; char *str; if (s == NULL) return (NULL); i = 0; while (*(s + i) == ' ' || *(s + i) == '\n' || *(s + i) == '\t') ++i; if (i == (int)ft_strlen(s)) { if (!(str = (char *)gmalloc(sizeof(char) * (1)))) return (NULL); *(str + 0) = '\0'; return (str); } j = (int)ft_strlen(s) - 1; while (*(s + j) == ' ' || *(s + j) == '\n' || *(s + j) == '\t') --j; if (!(str = (char *)gmalloc(sizeof(char) * (j - i + 1 + 1)))) return (NULL); str = ft_strsub(s, i, j - i + 1); return (str); }
static int ft_test(char **path, char **exe, char **env) { int i; struct stat *buf; char *tmp; i = -1; tmp = NULL; if (!(buf = (struct stat *)gmalloc(sizeof(*buf)))) return (-1); while (path[++i]) { tmp = ft_strjoin(path[i], "/"); gfree((void *)path[i]); path[i] = ft_strjoin(tmp, exe[0]); gfree((void *)tmp); if (lstat(path[i], buf) == 0 && buf->st_mode & S_IXUSR) break ; gfree((void *)buf); if (!(buf = (struct stat *)gmalloc(sizeof(*buf)))) return (-1); gfree((void *)path[i]); } if (!path[i]) return (0 - gfree((void *)path)); ft_execution(path[i], exe, env, buf); return (ft_test_ok(path, i)); }
int main() { llist ma_liste = NULL; /*Initialisation de la liste*/ int i=1; /*Variable compteur*/ char* doubleGmalloc = NULL; /*Pointeurs de test*/ char * pointeur = NULL ; printf("\nTest d'allocation simple\n\n"); pointeur = gmalloc(20 * sizeof (char)); if (pointeur == NULL) { printf("L'allocation n'a pu être réalisée\n"); } else { printf("L'allocation a été un succès\n"); gfree(pointeur); } printf("\nTest d'allocation liste chainée \n\n"); for(i=1;i<=1000;i++) { ma_liste = ajouterEnTete(ma_liste, i); ma_liste = ajouterEnFin(ma_liste, i); } printf("\nOn s'attend à l'affichage de 1000 valeurs croissantes puis 1000 décroissantes\n\n"); afficherListe(ma_liste); effacerListe(ma_liste); printf("\nDouble gmalloc\n\n"); doubleGmalloc=gmalloc(20 * sizeof (char)); doubleGmalloc="Test malloc 1 \n"; printf("Après gmalloc 1 : pointeur : 0x%x , Valeur : %s\n",*doubleGmalloc,doubleGmalloc); doubleGmalloc=gmalloc(20 * sizeof (char)); doubleGmalloc="Test malloc 2 \n"; printf("Après gmalloc 2 : pointeur : 0x%x , Valeur : %s\n",*doubleGmalloc,doubleGmalloc); printf("\nDouble gfree\n\n On s'attend à une segmentation fault !\n\n"); gfree(doubleGmalloc); gfree(doubleGmalloc); return 0; }
struct net_device* alloc_etherdev(int sizeof_priv) { struct net_device* dev = gmalloc(sizeof(struct net_device)); bzero(dev, sizeof(struct net_device)); // memcpy(dev->name, "eth0", 5); dev->mtu = 1500; dev->addr_len = ETH_ALEN; dev->priv = gmalloc(sizeof_priv); bzero(dev->priv, sizeof_priv); return dev; }
int launchSimulationSensor(char * id, int sleepingTime, SimuThreadList ** pp_simuThreadList, enum SIMU_TYPE simu_type) { SimuThreadList* p_simuThread; void *(*SimulationSensorFunction)(void *ptr); ArgSensor * argSensor = (ArgSensor*) gmalloc(sizeof (ArgSensor)); strncpy(argSensor->id, id, 9); argSensor->sleepingTime = sleepingTime; switch (simu_type) { case SIMU_TEMP: SimulationSensorFunction = SimulationSensorTemp; break; case SIMU_SWITCH: SimulationSensorFunction = SimulationSensorSwitch; break; case SIMU_CONTACT: SimulationSensorFunction = SimulationSensorContact; break; case SIMU_LIGHT: SimulationSensorFunction = SimulationSensorLight; break; case SIMU_OCCUPANCY: SimulationSensorFunction = SimulationSensorOccupancy; break; case SIMU_LIGHT_OCCUPANCY: SimulationSensorFunction = SimulationSensorLightOccupancy; break; default: printf("[launchSimulationSensor] Capteur non supporté pour la simulation.\n"); return ERROR; } if (*pp_simuThreadList == NULL) { /* Liste vide */ /* Creation du premier de la liste */ *pp_simuThreadList = (SimuThreadList*) gmalloc(sizeof (SimuThreadList)); (*pp_simuThreadList)->next = NULL; p_simuThread = *pp_simuThreadList; } else { p_simuThread = *pp_simuThreadList; /* Parcours de la liste jusqu a l'avant dernier élément de la liste */ while ((p_simuThread != NULL) && (p_simuThread->next != NULL)) { p_simuThread = p_simuThread->next; } /* Creation du capteur en fin de liste */ p_simuThread->next = (SimuThreadList*) gmalloc(sizeof (SimuThreadList)); p_simuThread = p_simuThread->next; p_simuThread->next = NULL; } pthread_create(&(p_simuThread->thread), NULL, SimulationSensorFunction, (void*) argSensor); return OK; }
char *strdup_and_subst_node(char *str, Agnode_t * n) { char c, *s, *p, *t, *newstr; char *g_str = NULL, *n_str = NULL; int g_len = 0, n_len = 0, newlen = 0; /* two passes over str. * * first pass prepares substitution strings and computes * total length for newstring required from malloc. */ for (s = str; (c = *s++);) { if (c == '\\') { switch (c = *s++) { case 'G': if (!g_str) { g_str = n->graph->name; g_len = strlen(g_str); } newlen += g_len; break; case 'N': if (!n_str) { n_str = n->name; n_len = strlen(n_str); } newlen += n_len; break; default: newlen += 2; } } else { newlen++; } } /* allocate new string */ newstr = gmalloc(newlen + 1); /* second pass over str assembles new string */ for (s = str, p = newstr; (c = *s++);) { if (c == '\\') { switch (c = *s++) { case 'G': for (t = g_str; (*p = *t++); p++); break; case 'N': for (t = n_str; (*p = *t++); p++); break; default: *p++ = '\\'; *p++ = c; } } else { *p++ = c; } } *p++ = '\0'; return newstr; }
llist ajouterEnFin(llist liste, int valeur) { element* nouvelElement = gmalloc(sizeof(element)); nouvelElement->val = valeur; nouvelElement->nxt = NULL; if(liste == NULL) { return nouvelElement; } else { element* temp=liste; while(temp->nxt != NULL) { temp = temp->nxt; } temp->nxt = nouvelElement; return liste; } }
char *ft_itoa(int n) { char *s; int i; int k; i = 0; k = n; if (!(s = (char *)gmalloc(sizeof(*s) * 12))) return (NULL); if (n == -2147483648) return (ft_strcpy(s, "-2147483648")); if (n == 0) { *s = '0'; *(s + 1) = '\0'; return (s); } while ((k > 9) || (k < -9)) { i++; k = k / 10; } return (ft_itoa_next(n, s, i++)); }
int ft_init0(t_sh **sh, t_sh2 **sh2, t_mlx *mlx, pthread_t **tab) { int i; if (!(*sh = (t_sh *)gmalloc(sizeof(t_sh) * (NB_PHILO + 1))) || !(*sh2 = (t_sh2 *)gmalloc(sizeof(t_sh2) * (NB_PHILO + 1))) || !(*tab = (pthread_t *)gmalloc(sizeof(pthread_t) * (NB_PHILO + 1)))) return (-1); i = -1; while (++i < (NB_PHILO + 1)) { (*sh)[i].id = i; } (*sh)[i - 1].mlx = mlx; return (0); }
WindowSize *getTermSize(int fd) { WindowSize *ret = (WindowSize *)gmalloc(sizeof(WindowSize)); #ifdef TIOCGSIZE struct ttysize win; if (ioctl(fd, TIOCGSIZE, &win)) { return ret; } ret->width = win.ts_cols; ret->height = win.ts_lines; #elif defined(TIOCGWINSZ) struct winsize win; if (ioctl(fd, TIOCGWINSZ, &win)) { return ret; } ret->width = win.ws_col; ret->height = win.ws_row; #else char *line = getenv("COLUMNS"); if (line) { ret->width = strtol(line, NULL, 10); } else { ret->width = 80; } line = getenv("LINES"); if (line) { ret->height = strtol(line, NULL, 10); } else { ret->height = 25; } #endif return ret; }
/** * Allocates bytesize bytes of linear memory on the device and returns in * @dptr a pointer to the allocated memory. The allocated memory is suitably * aligned for any kind of variable. The memory is not cleared. If bytesize * is 0, cuMemAlloc() returns CUDA_ERROR_INVALID_VALUE. * * Parameters: * dptr - Returned device pointer * bytesize - Requested allocation size in bytes * * Returns: * CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_NOT_INITIALIZED, * CUDA_ERROR_INVALID_CONTEXT, CUDA_ERROR_INVALID_VALUE, * CUDA_ERROR_OUT_OF_MEMORY */ CUresult cuMemAlloc_v2(CUdeviceptr *dptr, unsigned int bytesize) { CUresult res; struct CUctx_st *ctx; Ghandle handle; uint64_t addr; uint64_t size = bytesize; if (!gdev_initialized) return CUDA_ERROR_NOT_INITIALIZED; res = cuCtxGetCurrent(&ctx); if (res != CUDA_SUCCESS) return res; if (!dptr) return CUDA_ERROR_INVALID_VALUE; handle = ctx->gdev_handle; if (!(addr = gmalloc(handle, size))) { return CUDA_ERROR_OUT_OF_MEMORY; } *dptr = addr; return CUDA_SUCCESS; }
void gmem_test3() { int *table[10]; short boolMemAllocated[10]; int i; int size[4] = {4U, 6U, 8U, 10U}; unsigned initialAvailableMem = gmem_availableMem(); printf("Test 3 : running.\n"); for (i = 0; i < 10; ++i) { table[i] = NULL; boolMemAllocated[i] = 0; } srand(time(NULL)); for (i = 0; i < __GMEM_FUNCTIONAL_TEST_TRIES; ++i) { int j = rand() % 10; printf("\tFunctional %d running\n", i); if (boolMemAllocated[j]) { printf("\t\tFree.\n"); gfree(table[j]); boolMemAllocated[j] = 0; } else { int sizeChosen = size[rand()%4]; unsigned memSize = sizeof(int)*sizeChosen; printf("\t\tMalloc of %d.\n", memSize); table[j] = (int*) gmalloc(memSize); assert(table[j] != NULL); gmem_fill_tab(table[j], sizeChosen); boolMemAllocated[j] = 1; } # ifdef __DEBUG gmem_show_heap(); # endif } for (i = 0; i < 10; ++i) { if (boolMemAllocated[i]) { gfree(table[i]); } } assert(gmem_availableMem() == initialAvailableMem); assert(gmem_sizeFreeBlockList() == 1); assert((void*)((unsigned)heap + __HEAP_SIZE - 1U) == (void*)((unsigned)firstHdr->blockNode.mem + firstHdr->blockNode.size - 1U)); printf("Test 3 : OK.\n"); }
void gmem_test1() { int* table = 0; int tableSize = 64; unsigned mem1 = sizeof(int)*tableSize; unsigned initialAvailableMem = gmem_availableMem(); printf("Test 1 : running.\n"); table = (int*) gmalloc(mem1); assert(table != NULL); assert(gmem_availableMem() == __HEAP_SIZE - 2*hdrSize - mem1); gmem_fill_tab(table, tableSize); gfree(table); assert(gmem_availableMem() == initialAvailableMem); assert(gmem_sizeFreeBlockList() == 1); assert((void*)((unsigned)heap + (unsigned)__HEAP_SIZE - 1U) == (void*)((unsigned)firstHdr->blockNode.mem + firstHdr->blockNode.size -1U)); printf("Test 1 : OK.\n"); }
t_env *ft_cd(char *line, t_env *e) { int i; char *buf; char **t; t = ft_strsplim(line); buf = (char *)gmalloc(sizeof(char) * 20000); getcwd(buf, 20000); i = 0; if (t[1] && !t[2] && chdir(t[1]) >= 0) return (ft_actualize(e, buf, t, 1)); if (!t[1]) { while (e->env[i] && ft_strncmp(e->env[i], "HOME", 4) != 0) ++i; if (e->env[i]) chdir(&(e->env[i][5])); } else if (!ft_strcmp(t[1], "-") && !t[2]) { while (e->env[i] && ft_strncmp(e->env[i], "OLDPWD", 6) != 0) ++i; if (e->env[i]) chdir(&(e->env[i][7])); } return (ft_actualize(e, buf, t, i)); }
void* dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp) { uint64_t align; if(size <= 0) return NULL; /* Check the size is whether to be square of 2 or not */ if((size & (size - 1)) != 0) align = 1 << PAGE_SHIFT; // default is 4kb alignment else align = size; void *ptr; void *p = gmalloc(size + align - 1 + sizeof(void*)); if (p != NULL) { /* Address of the aligned memory according to the align parameter*/ ptr = (void*)(((uint64_t)p + sizeof(void*) + align -1) & ~(align-1)); /* store the address of the malloc() above * at the beginning of our total memory area. * You can also use *((void **)ptr-1) = p * instead of the one below. */ *((void**)((uint64_t)ptr - sizeof(void*))) = p; /* Return the address of aligned memory */ *dma_handle = (dma_addr_t)ptr; return ptr; } return NULL; }
int ft_executable(char *line, char **env) { struct stat *buf; char *path; char **paths; char **exe; int i; i = -1; if (!(buf = (struct stat *)gmalloc(sizeof(*buf)))) return (-1); exe = ft_strsplim(line); while (env[++i] && ft_strncmp(env[i], "PATH=", 5)) ; path = env[i] ? ft_strdup(&env[i][5]) : NULL; paths = env[i] ? ft_strsplit(path, ':') : NULL; if (path && ft_test(paths, exe, env) != 1) { gfree((void *)path); if (lstat(exe[0], buf) == 0 && buf->st_mode & S_IXUSR) return (ft_execution(line, exe, env, buf)); gfree((void *)buf); return (-1 + 0 * (write(2, line, ft_strlen(line)) + write(2, ": command not found or permission denied\n" , 41))); } return (0); }
static void GRowColOrderIt(GRowCol *grc) { if ( grc->order_entries ) { /* Sort each entry left to right then top to bottom (1 2 3/4 5 6/...) */ qsort(grc->ti,grc->rows*grc->cols,sizeof(GTextInfo *),grc->orderer); if ( grc->backwards ) { int i; GTextInfo *ti; for ( i=0; i<grc->ltot/2; ++i ) { ti = grc->ti[i]; grc->ti[i] = grc->ti[grc->ltot-1-i]; grc->ti[grc->ltot-1-i] = ti; } } } else { /* Sort each row, top to bottom (1 8 5/2 7 3/...) */ qsort(grc->ti,grc->rows,sizeof(GTextInfo *)*grc->cols,grc->orderer); if ( grc->backwards ) { int i; GTextInfo **ti; ti = gmalloc(grc->cols*sizeof(GTextInfo *)); for ( i=0; i<grc->rows/2; ++i ) { memcpy(ti,grc->ti+(i*grc->cols),grc->cols*sizeof(GTextInfo *)); memcpy(grc->ti+(i*grc->cols),grc->ti+((grc->rows-1-i)*grc->cols),grc->cols*sizeof(GTextInfo *)); memcpy(grc->ti+((grc->rows-1-i)*grc->cols),ti,grc->cols*sizeof(GTextInfo *)); } free(ti); } } }
char *ft_find_the_path(char *cmd, char **paths) { char *good_path; struct stat *buf; int i; good_path = NULL; buf = NULL; if ((good_path = ft_not_need_variable_env_path(cmd))) return (good_path); i = -1; while (paths[++i]) { if (!(buf = (struct stat *)gmalloc(sizeof(*buf)))) return (NULL); good_path = ft_strjoin(ft_strjoin(paths[i], "/"), cmd); if (lstat(good_path, buf) == 0 && buf->st_mode & S_IXUSR) { gfree(buf); return (good_path); } gfree(good_path); gfree(buf); } return (NULL); }
static int ft_token_without_backslashes(char **tok) { char *ret; int i; int j; if (!(ret = (char *)gmalloc(sizeof(char) * (ft_strlen(*tok) + 1)))) return (-1); j = -1; i = -1; while ((*tok)[++i]) { if ((*tok)[i] == '\\') { if ((*tok)[++i]) ret[++j] = (*tok)[i]; else return (-2); } else ret[++j] = (*tok)[i]; } ret[++j] = '\0'; gfree((void *)(*tok)); *tok = ft_strdup(ret); gfree((void *)ret); return (0); }
HashTable* new_HashTable(void) { HashTable *h_table = (HashTable *)gmalloc(sizeof(HashTable)); h_table->key = NULL; h_table->value = 0; h_table->next = NULL; return h_table; }
tFunction* BSTF_Insert(tFunction ** node, char * key, inbuiltFunction * function) { if(*node == NULL) { tFunction * tmp; if((tmp = malloc(sizeof(tFunction))) != NULL) { *node = tmp; (*node)->key = malloc(strlen(key)+1); if((*node)->key == NULL) printError(ALLOCERROR, INTERPRETERROR); memcpy((*node)->key, key, strlen(key)+1); (*node)->lptr = (*node)->rptr = NULL; (*node)->codePosition = 0; (*node)->function = function; if(function == NULL) { (*node)->paramNames = gmalloc(sizeof(sString), free); stackStringInit((*node)->paramNames, 5); BSTV_Init(&(*node)->variables); (*node)->code = gmalloc(sizeof(sInstruction), free); stackInstructionInit((*node)->code, 50); } return *node; } else { printError(ALLOCERROR, INTERPRETERROR); return NULL; } } else { int compare = strcmp(key, (*node)->key); if(compare > 0) { return BSTF_Insert(&(*node)->rptr, key, function); } else if (compare < 0) { return BSTF_Insert(&(*node)->lptr, key, function); } else { printError(FUNCTIONEXISTS, FUNCTIONDEFINITIONERROR); return NULL; } } }
int main(int argc, char **argv) { char* a = gmalloc(10); a = "salut"; printf("%s\n", a); gfree(a); return(0); }
static gvplugin_package_t * gvplugin_package_record(GVC_t * gvc, char *path, char *name) { gvplugin_package_t *package = gmalloc(sizeof(gvplugin_package_t)); package->path = (path) ? strdup(path) : NULL; package->name = strdup(name); package->next = gvc->packages; gvc->packages = package; return package; }
int ft_init_disk1(t_obj *obj) { t_dis **ldis; int i; obj->nb[DIS] = 4; if (!(((t_dis ***)obj->type)[DIS] = (t_dis **)gmalloc(sizeof(t_dis *) * obj->nb[DIS]))) return (-1); ldis = ((t_dis ***)obj->type)[DIS]; i = -1; while (++i < obj->nb[DIS]) { if (!(ldis[i] = (t_dis *)gmalloc(sizeof(t_dis)))) return (-1); } ft_init_disk1_2(ldis); return (0); }
static int ft_no_new_line(char **line, char *buf, int j) { char *tmp; if (!(tmp = (char *)gmalloc(sizeof(*tmp) * BUFF_SIZE * j + 1))) return (-1); ft_strclr(tmp); ft_strcpy(tmp, *line); gfree((void *)*line); j++; if (!(*line = (char *)gmalloc(sizeof(**line) * BUFF_SIZE * j + 1))) return (-1); ft_strclr(*line); ft_strcpy(*line, tmp); gfree((void *)tmp); ft_strcat(*line, buf); buf[0] = '\0'; return (j); }
t_link *ft_new_link(void) { t_link *link; if (!(link = (t_link *)gmalloc(sizeof(t_link)))) return (NULL); link->name = NULL; link->next = NULL; link->prev = NULL; return (link); }