static char * allocate_heap (void) { /* Try to get as much as possible of the address range from the end of the preload heap section up to the usable address limit. Since GNU malloc can handle gaps in the memory it gets from sbrk, we can simply set the sbrk pointer to the base of the new heap region. */ DWORD_PTR base = ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress) + preload_heap_section->Misc.VirtualSize), get_allocation_unit ()); DWORD_PTR end = ((unsigned __int64)1) << VALBITS; /* 256MB */ void *ptr = NULL; while (!ptr && (base < end)) { #ifdef _WIN64 reserved_heap_size = min(end - base, 0x4000000000i64); /* Limit to 256Gb */ #else reserved_heap_size = end - base; #endif ptr = VirtualAlloc ((void *) base, get_reserved_heap_size (), MEM_RESERVE, PAGE_NOACCESS); base += 0x00100000; /* 1MB increment */ } return ptr; }
void * mmap_alloc (void **var, size_t nbytes) { void *p = NULL; /* We implement amortized allocation. We start by reserving twice the size requested and commit only the size requested. Then realloc could proceed and use the reserved pages, reallocating only if needed. Buffer shrink would happen only so that we stay in the 2x range. This is a big win when visiting compressed files, where the final size of the buffer is not known in advance, and the buffer is enlarged several times as the data is decompressed on the fly. */ if (nbytes < MAX_BUFFER_SIZE) p = VirtualAlloc (NULL, ROUND_UP (nbytes * 2, get_allocation_unit ()), MEM_RESERVE, PAGE_READWRITE); /* If it fails, or if the request is above 512MB, try with the requested size. */ if (p == NULL) p = VirtualAlloc (NULL, ROUND_UP (nbytes, get_allocation_unit ()), MEM_RESERVE, PAGE_READWRITE); if (p != NULL) { /* Now, commit pages for NBYTES. */ *var = VirtualAlloc (p, nbytes, MEM_COMMIT, PAGE_READWRITE); if (*var == NULL) p = *var; } if (!p) { DWORD e = GetLastError (); if (e == ERROR_NOT_ENOUGH_MEMORY) errno = ENOMEM; else { DebPrint (("mmap_alloc: error %ld\n", e)); errno = EINVAL; } } return *var = p; }
/* SYNOPSYS : * int syr1_fopen_write(char *name, SYR1_FILE *file) { * DESCRIPTION : * Ce sous-programme gère l'ouverture d'un fichier logique en mode écriture. * PARAMETRES : * name : chaîne de caratère contenant le nom externe du fichier à ouvrir * file : pointeur sur un Bloc Control Fichier (File Control Bloc) * RESULTAT : * 0 : ouverture réussie * -1 : autre erreur */ int syr1_fopen_write(char *name, SYR1_FILE *file) { int error = 0; //Si le fichier existe déjà, suppression if(search_entry(name, &(file->descriptor)) == 0) remove_entry(name); //initialisation du descripteur strcpy(file->descriptor.name, name); file->descriptor.size = 0; file->descriptor.alloc[0] = get_allocation_unit(); //création du fichier sur disque create_entry(name, &(file->descriptor)); //initialisation du BCF strcpy(file->mode, "w"); file->current_block = 0; file->file_offset = 0; file->block_offset = 0; file->buffer = malloc(512*sizeof(unsigned char)); return -1; }