my_list_t *my_list_create(void) { my_list_t *list; list = my_mem_alloc(sizeof(*list)); return list; }
int my_list_enqueue_tail(my_list_t *list, void *data) { my_node_t *node; node = my_mem_alloc(sizeof(*node)); if (node) { my_list_add_tail(list, node); node->data = data; return 0; } return -1; }
void my_list_insert_before(my_list_t *list, my_node_t *node, void *data) { my_node_t *new_node; new_node = my_mem_alloc(sizeof(*new_node)); if (!new_node) return; new_node->data = data; if (!node->prev) { list->head = new_node; new_node->prev = NULL; } else { new_node->prev = node->prev; node->prev->next = new_node; } new_node->next = node; node->prev = new_node; }
int main() { my_mem_init(memoire, TAILLE_MEMOIRE); printf("Test de l'etat initial de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 1); printf("TEST INIT OK\n\n"); printf("\n--------------------------\n\n"); printf("Test de free :\n"); mem_fit(&mem_fit_best); my_mem_alloc(500); my_mem_alloc(400); my_mem_alloc(300); my_mem_alloc(200); //my_mem_free(0); my_mem_free(1); //my_mem_free(2); my_mem_free(3); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); printf("nb_free = %d ; nb_busy = %d\n", nb_free, nb_busy); assert(nb_free == 2 && nb_busy == 2); printf("TEST FREE OK\n\n"); my_mem_init(memoire, TAILLE_MEMOIRE); printf("\n--------------------------\n\n"); printf("Test de best fit :\n"); my_mem_alloc(500); my_mem_alloc(400); my_mem_alloc(300); my_mem_alloc(200); my_mem_alloc(100); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(400); my_mem_alloc(500); printf("Etat de la memoire :\n"); my_mem_free(0); my_mem_free(2); my_mem_free(4); my_mem_free(7); my_mem_alloc(200); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); printf("nb_free = %d ; nb_busy = %d\n", nb_free, nb_busy); assert(nb_free == 5 && nb_busy == 6); printf("TEST BEST OK\n\n"); my_mem_init(memoire, TAILLE_MEMOIRE); printf("\n--------------------------\n\n"); printf("Test de worst fit :\n"); mem_fit(&mem_fit_worst); my_mem_alloc(500); my_mem_alloc(400); my_mem_alloc(300); my_mem_alloc(200); my_mem_alloc(100); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(400); my_mem_alloc(500); my_mem_alloc(1096); my_mem_free(0);//Libere 500 my_mem_free(2);//Libere 300 my_mem_free(4);//Libere 100 my_mem_alloc(200); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); printf("nb_free = %d ; nb_busy = %d\n", nb_free, nb_busy); assert(nb_free == 3 && nb_busy == 8); printf("TEST WORST OK\n\n"); printf("\n--------------------------\n\n"); printf("1.Best Fit est meilleur que First Fit :\n"); printf(" 1.1 Test avec Best fit :\n"); my_mem_init(memoire, TAILLE_MEMOIRE); mem_fit(&mem_fit_best); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(100); my_mem_alloc(3456); my_mem_free(0);//Libere 200 my_mem_free(2);//Libere 100 my_mem_alloc(90); my_mem_alloc(190); my_mem_show(afficher_zone); printf("\n 1.2 Test avec First fit :\n"); my_mem_init(memoire, TAILLE_MEMOIRE); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(100); my_mem_alloc(3456); my_mem_free(0);//Libere 200 my_mem_free(2);//Libere 100 my_mem_alloc(90); my_mem_alloc(190);//Allocaionimpossible avec first fit my_mem_show(afficher_zone); printf("\n2.First Fit est meilleur que Best Fit :\n"); printf(" 2.1 Test avec First fit :\n"); my_mem_init(memoire, TAILLE_MEMOIRE); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(100); my_mem_alloc(3456); my_mem_free(0);//Libere 200 my_mem_free(2);//Libere 100 my_mem_alloc(72); my_mem_alloc(112); my_mem_alloc(92); my_mem_show(afficher_zone); printf(" 2.2 Test avec Best fit :\n"); my_mem_init(memoire, TAILLE_MEMOIRE); mem_fit(&mem_fit_best); my_mem_alloc(200); my_mem_alloc(300); my_mem_alloc(100); my_mem_alloc(3456); my_mem_free(0);//Libere 200 my_mem_free(2);//Libere 100 my_mem_alloc(72); my_mem_alloc(112);//Allocation impossible avec best_fit my_mem_alloc(92); my_mem_show(afficher_zone); }
int main() { size_t struct_fb_size, struct_bb_size; int i, j, nb_alloc = 0, size; char *result; struct timeval tv; my_mem_init(memoire, TAILLE_MEMOIRE); printf("Test de l'etat initial de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 1); printf("TEST OK\n\n"); printf("Test de base, serie d'allocations :\n"); my_mem_alloc(8); my_mem_alloc(16); my_mem_alloc(4); my_mem_alloc(12); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 1 && nb_busy == 4); printf("TEST OK\n\n"); printf("Test de mem_init :\n"); my_mem_init(memoire, TAILLE_MEMOIRE); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 1 && nb_busy == 0); printf("TEST OK\n\n"); printf("Test d'allocation puis liberation :\n"); my_mem_alloc(4); my_mem_alloc(4); my_mem_alloc(30); my_mem_alloc(1); my_mem_alloc(64); my_mem_alloc(24); my_mem_alloc(23); my_mem_free(2); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 2 && nb_busy == 6); printf("TEST OK\n\n"); printf("Verification de la taille des allocations (multiples de %zd)\n", sizeof(void*)); for (i=0; i<nb_busy; i++) assert((busy_size[i] & (sizeof(void*)-1)) == 0); printf("TEST OK\n\n"); { int i=1; intptr_t tmp = ~mem_align; while (tmp & 1) { tmp >>= 1; i <<= 1; } printf("L'alignement semble être systématique sur %i octets\n", i); } printf("Test de fusion amont :\n"); my_mem_free(1); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); struct_fb_size = free_size[0] - 36; assert(nb_free == 2 && nb_busy == 5); printf("TEST OK\n\n"); printf("Test de fusion aval :\n"); my_mem_free(3); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 2 && nb_busy == 4); printf("TEST OK\n\n"); printf("Test de fusion amont+aval :\n"); my_mem_free(5); my_mem_show(decompte_zone); assert(nb_free == 3 && nb_busy == 3); my_mem_free(4); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 2 && nb_busy == 2); printf("TEST OK\n\n"); printf("Récupération de la taille d'entête de bloc occupé:\n"); my_mem_init(memoire, TAILLE_MEMOIRE); my_mem_show(decompte_zone); struct_bb_size = free_size[0]; my_mem_alloc(__BIGGEST_ALIGNMENT__); my_mem_show(decompte_zone); struct_bb_size -= free_size[0] + __BIGGEST_ALIGNMENT__; printf("Apparemment la taille de votre entete de bloc occupe est de %lu " "octets\n", (unsigned long) struct_bb_size); printf("\n"); printf("Test d'allocation/liberation de tout l'espace :\n"); my_mem_init(memoire, TAILLE_MEMOIRE); my_mem_alloc(TAILLE_MEMOIRE - struct_bb_size); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 0 && nb_busy == 1); my_mem_free(0); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 1 && nb_busy == 0); printf("TEST OK\n\n"); printf("Récupération de la taille d'entête de bloc libre:\n"); my_mem_init(memoire, TAILLE_MEMOIRE); my_mem_alloc(4*__BIGGEST_ALIGNMENT__); my_mem_alloc(TAILLE_MEMOIRE - 2*struct_bb_size - 4*__BIGGEST_ALIGNMENT__); my_mem_show(afficher_zone); assert(nb_free == 0 && nb_busy == 2); my_mem_free(0); my_mem_show(decompte_zone); assert(nb_free == 1 && nb_busy == 1); for (i=1; i<4*__BIGGEST_ALIGNMENT__; i++) { my_mem_alloc(4*__BIGGEST_ALIGNMENT__ - i); my_mem_show(decompte_zone); assert((nb_free == 1 || nb_free == 0) && nb_busy == 2); if (nb_free == 1) { break; } my_mem_free(i+1); } struct_fb_size=i; printf("Apparemment la taille de votre entete de bloc libre est de %lu " "octets\n", (unsigned long) struct_fb_size); printf("\n"); printf("Test d'allocation/libération avec utilisation de la mémoire :\n"); gettimeofday(&tv, NULL); srand(tv.tv_usec); my_mem_init(memoire, TAILLE_MEMOIRE); my_mem_alloc(16); my_mem_alloc(4); my_mem_alloc(30); my_mem_alloc(1); my_mem_alloc(64); my_mem_alloc(24); my_mem_alloc(23); for (i=0; i<position; i++) for (j=0; j<sizes[i]; j++) ((char *) allocs[i])[j] = (char) rand(); my_mem_free(1); my_mem_free(3); my_mem_free(5); my_mem_alloc(4); for (i=0; i<position; i++) if ((i > 5) || (i & 1) == 0) for (j=0; j<sizes[i]; j++) ((char *) allocs[i])[j] = (char) rand(); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); /* Dans le cas de la politique worst_fit, nb_free est à 4 * il est à 3 dans les autres cas */ assert( (nb_free == 3 || nb_free == 4) && nb_busy == 5); printf("TEST OK\n\n"); printf("Test final, serie aléatoire d'allocations/libérations aléatoires " "avec utilisation de la mémoire :\n"); my_mem_init(memoire, TAILLE_MEMOIRE); for (i=0; i<1000; i++) { if (nb_alloc && (rand() & 1)) { for (j=0; !allocs[j]; j++) { } my_mem_free(j); allocs[j] = NULL; nb_alloc--; } else { size = rand() & 511; result = my_mem_alloc(size); if (result) { for (j=0; j<size; j++) result[j] = (char) rand(); nb_alloc++; } } } printf("TEST OK\n\n"); return 0; }
int main() { int struct_fb_size, struct_bb_size; int i, j, nb_alloc = 0, size, addr; char *result; my_mem_init(TAILLE_MEMOIRE); printf("Test de l'etat initial de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 1); printf("TEST OK\n\n"); printf("Test de base, serie d'allocations :\n"); my_mem_alloc(8); my_mem_alloc(16); my_mem_alloc(4); my_mem_alloc(12); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 1 && nb_busy == 4); printf("TEST OK\n\n"); printf("Test de mem_reinit :\n"); my_mem_init(-1); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 1); printf("TEST OK\n\n"); printf("Test d'allocation puis liberation :\n"); struct_bb_size = free_size[0]; my_mem_alloc(16); my_mem_show(decompte_zone); struct_bb_size -= free_size[0] + 16; my_mem_alloc(4); my_mem_alloc(30); my_mem_alloc(1); my_mem_alloc(64); my_mem_alloc(24); my_mem_alloc(23); my_mem_free(2); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 2 && nb_busy == 6); printf("TEST OK\n\n"); printf("Verification de la taille des allocations (multiples de 4)\n"); for (i = 0; i < nb_busy; i++) assert((busy_size[i] & 3) == 0); printf("TEST OK\n\n"); printf("Test de fusion amont :\n"); my_mem_free(1); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); struct_fb_size = free_size[0] - 36; assert(nb_free == 2 && nb_busy == 5); printf("TEST OK\n\n"); printf("Test de fusion aval :\n"); my_mem_free(3); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 2 && nb_busy == 4); printf("TEST OK\n\n"); printf("Test de fusion amont+aval :\n"); my_mem_free(5); my_mem_show(decompte_zone); assert(nb_free == 3 && nb_busy == 3); my_mem_free(4); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 2 && nb_busy == 2); printf("TEST OK\n\n"); addr = (int) struct_fb_size; printf("Apparemment la taille de votre entete de bloc libre est de %d " "octets\n", &addr); addr = (int) struct_bb_size; printf("Apparemment la taille de votre entete de bloc occupe est de %d " "octets\n", &addr); printf("\n"); printf("Test d'allocation/liberation de tout l'espace :\n"); my_mem_init(-1); my_mem_alloc(TAILLE_MEMOIRE - struct_bb_size); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 0 && nb_busy == 1); my_mem_free(0); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 1 && nb_busy == 0); printf("TEST OK\n\n"); printf("Test d'allocation/libération avec utilisation de la mémoire :\n"); my_mem_init(-1); my_mem_alloc(16); my_mem_alloc(4); my_mem_alloc(30); my_mem_alloc(1); my_mem_alloc(64); my_mem_alloc(24); my_mem_alloc(23); for (i = 0; i < position; i++) for (j = 0; j < sizes[i]; j++) ((char *) allocs[i])[j] = (char) Random(); my_mem_free(1); my_mem_free(3); my_mem_free(5); my_mem_alloc(4); for (i = 0; i < position; i++) if ((i > 5) || (i & 1) == 0) for (j = 0; j < sizes[i]; j++) ((char *) allocs[i])[j] = (char) Random(); printf("Etat de la memoire :\n"); my_mem_show(afficher_zone); assert(nb_free == 3 && nb_busy == 5); printf("TEST OK\n\n"); printf("Test final, serie aléatoire d'allocations/libérations aléatoires " "avec utilisation de la mémoire :\n"); my_mem_init(-1); for (i = 0; i < 1000; i++) { if (nb_alloc && (Random() & 1)) { for (j = 0; !allocs[j]; j++) { } my_mem_free(j); allocs[j] = NULL; nb_alloc--; } else { size = Random() & 511; result = my_mem_alloc(size); if (result) { for (j = 0; j < size; j++) result[j] = (char) Random(); nb_alloc++; } } } printf("TEST OK\n\n"); return 0; }
int main() { size_t struct_fb_size, struct_bb_size; int i; my_mem_init(TAILLE_MEMOIRE); my_mem_show(afficher_zone); if(nb_free != 1) { PutString("assert failed ! 78\n"); Exit(0); } PutString("TEST OK\n\n"); my_mem_alloc(8); my_mem_alloc(16); my_mem_alloc(4); my_mem_alloc(12); PutString("Etat de la memoire :\n"); my_mem_show(afficher_zone); if(nb_free != 1 || nb_busy != 4) { PutString("assert failed ! 92\n"); Exit(0); } my_mem_init(TAILLE_MEMOIRE); my_mem_show(afficher_zone); if(nb_free != 1 || nb_busy != 0) { PutString("assert failed ! l89\n"); Exit(0); } PutString("TEST OK\n\n"); my_mem_alloc(4); my_mem_alloc(4); my_mem_alloc(30); my_mem_alloc(1); my_mem_alloc(64); my_mem_alloc(24); my_mem_alloc(23); my_mem_free(2); my_mem_show(afficher_zone); if(nb_free != 2 || nb_busy != 6) { PutString("assert failed ! l108\n"); Exit(0); } PutString("TEST OK\n\n"); { int al=1; unsigned int tmp = ~mem_align; while (tmp & 1) { tmp >>= 1; al <<= 1; } } my_mem_free(1); my_mem_show(afficher_zone); struct_fb_size = free_size[0] - 36; if(nb_free != 2 || nb_busy != 5) { PutString("assert failed ! 134\n"); Exit(0); } PutString("TEST OK\n\n"); my_mem_free(3); my_mem_show(afficher_zone); if(nb_free != 2 || nb_busy != 4) { PutString("assert failed ! 144\n"); Exit(0); } PutString("TEST OK\n\n"); my_mem_free(5); my_mem_show(decompte_zone); if(nb_free != 3 || nb_busy != 3) { PutString("assert failed ! 153\n"); Exit(0); } my_mem_free(4); my_mem_show(afficher_zone); if(nb_free != 2 || nb_busy != 2) { PutString("assert failed ! 160\n"); Exit(0); } PutString("TEST OK\n\n"); my_mem_init(TAILLE_MEMOIRE); my_mem_show(decompte_zone); struct_bb_size = free_size[0]; my_mem_alloc(__BIGGEST_ALIGNMENT__); my_mem_show(decompte_zone); struct_bb_size -= free_size[0] + __BIGGEST_ALIGNMENT__; my_mem_init(TAILLE_MEMOIRE); my_mem_alloc(TAILLE_MEMOIRE - struct_bb_size); my_mem_show(afficher_zone); if(nb_free != 0 || nb_busy != 1) { PutString("assert failed ! 182\n"); Exit(0); } my_mem_free(0); my_mem_show(afficher_zone); if(nb_free != 1 || nb_busy != 0) { PutString("assert failed ! 189\n"); Exit(0); } PutString("TEST OK\n\n"); my_mem_init(TAILLE_MEMOIRE); my_mem_alloc(4*__BIGGEST_ALIGNMENT__); my_mem_alloc(TAILLE_MEMOIRE - 2*struct_bb_size - 4*__BIGGEST_ALIGNMENT__); my_mem_show(afficher_zone); if(nb_free != 0 || nb_busy != 2) { PutString("assert failed ! 210\n"); Exit(0); } my_mem_free(0); my_mem_show(decompte_zone); if(nb_free != 1 || nb_busy != 1) { PutString("assert failed ! 217\n"); Exit(0); } for (i=1; i<4*__BIGGEST_ALIGNMENT__; i++) { my_mem_alloc(4*__BIGGEST_ALIGNMENT__ - i); my_mem_show(decompte_zone); if((nb_free != 1 && nb_free != 0) || nb_busy != 2) { PutString("assert failed ! 221\n"); } if (nb_free == 1) { break; } my_mem_free(i+1); } return 0; }
static my_port_t *my_source_udp_create(my_core_t *core, my_port_conf_t *conf) { my_port_t *port; char *prop; struct in_addr ia; int ret; port = my_port_create_priv(MY_SOURCE_SIZE); if (!port) { goto _MY_ERR_create_priv; } prop = my_prop_lookup(conf->properties, "host"); if (!prop) { my_log(MY_LOG_ERROR, "core/%s: missing 'host' property", port->conf->name); goto _MY_ERR_conf; } ret = inet_pton(AF_INET, prop, &ia); if (ret != 1) { my_log(MY_LOG_ERROR, "core/%s: malformed 'host' property '%s'", port->conf->name, prop); goto _MY_ERR_conf; } MY_SOURCE(port)->ip_addr = prop; prop = my_prop_lookup(conf->properties, "port"); if (!prop) { my_log(MY_LOG_ERROR, "core/%s: missing 'port' property", port->conf->name); goto _MY_ERR_conf; } MY_SOURCE(port)->ip_port = atoi(prop); prop = my_prop_lookup(conf->properties, "audio-format"); MY_SOURCE(port)->codec = my_audio_codec_create(prop); if (!MY_SOURCE(port)->codec) { my_log(MY_LOG_ERROR, "core/%s: error creating audio codec '%s'", port->conf->name, prop ? prop : "(null)"); goto _MY_ERR_audio_codec_create; } MY_SOURCE(port)->sa_len = sizeof(struct sockaddr_in); MY_SOURCE(port)->sa_local = my_mem_alloc(MY_SOURCE(port)->sa_len); if (!MY_SOURCE(port)->sa_local) { goto _MY_ERR_alloc_sa_local; } my_mem_zero(MY_SOURCE(port)->sa_local, MY_SOURCE(port)->sa_len); MY_SOURCE(port)->sa_group = my_mem_alloc(MY_SOURCE(port)->sa_len); if (!MY_SOURCE(port)->sa_group) { goto _MY_ERR_alloc_sa_group; } my_mem_zero(MY_SOURCE(port)->sa_group, MY_SOURCE(port)->sa_len); ((struct sockaddr_in *)MY_SOURCE(port)->sa_local)->sin_family = AF_INET; ((struct sockaddr_in *)MY_SOURCE(port)->sa_local)->sin_addr.s_addr = htonl(INADDR_ANY); ((struct sockaddr_in *)MY_SOURCE(port)->sa_local)->sin_port = htons(MY_SOURCE(port)->ip_port); ((struct sockaddr_in *)MY_SOURCE(port)->sa_group)->sin_family = AF_INET; ((struct sockaddr_in *)MY_SOURCE(port)->sa_group)->sin_addr.s_addr = inet_addr(MY_SOURCE(port)->ip_addr); return port; my_mem_free(MY_SOURCE(port)->sa_group); _MY_ERR_alloc_sa_group: my_mem_free(MY_SOURCE(port)->sa_local); _MY_ERR_alloc_sa_local: my_audio_codec_destroy(MY_SOURCE(port)->codec); _MY_ERR_audio_codec_create: _MY_ERR_conf: my_port_destroy_priv(port); _MY_ERR_create_priv: return NULL; }