//------------------------------------------------------------------------------
// print the memory info for a single device
//
static void print_device_info(GLenum queryType, NVQRQueryDeviceInfo *devInfo)
{
    printf("number of memory resource allocations = %d\n",
           devInfo->summary.totalAllocs);

    if (devInfo->summary.totalAllocs > 0) {
        print_alloc_info("vid", devInfo->summary.vidMemUsedkiB,
                         devInfo->summary.vidMemFreekiB);
        if (queryType == GL_QUERY_RESOURCE_TYPE_DETAILED_NVX) {
            print_detailed_infos(devInfo, GL_QUERY_RESOURCE_MEMTYPE_VIDMEM_NVX);
        }
        print_alloc_info("sys", devInfo->summary.sysMemUsedkiB,
                         devInfo->summary.sysMemFreekiB);
        if (queryType == GL_QUERY_RESOURCE_TYPE_DETAILED_NVX) {
            print_detailed_infos(devInfo, GL_QUERY_RESOURCE_MEMTYPE_SYSMEM_NVX);
        }
    }
}
int main()
{
  char *name = "xingzhe";
  char *name_new = alloc(8);

  str_copy(name, name_new);
  printf("new name is \"%s\"\n", name_new);
  print_alloc_info();

  char *str = "hello";
  ul len = str_len(str);
  int ilen = (int)len;
  char *str_new = alloc(ilen);

  str_copy(str, str_new);
  printf("new name is \"%s\"\n", str_new);
  print_alloc_info();

  return 0;
}
Beispiel #3
0
char *memory_alloc(int size) {
    if (size <= 0 || size > MEMORY_SIZE - sizeof(busy_block_s))
        return NULL;

    size = (size + sizeof(busy_block_s) >= sizeof(free_block_s) ? size : sizeof(free_block_s) - sizeof(busy_block_s));
    // because we still want to be able to free the block so the minimum amount we can allocate is sizeof(free_block_s)
    // but here we subtract sizeof(busy_block_s) as we don't count it into the size variable

	free_block_t current;
	free_block_t prev = find_first_fit(size);
    if (prev == NULL) {
        // can not allocate memory
        print_alloc_info(NULL, size);
        return NULL;
    }

    if (prev != (free_block_t) (memory + MEMORY_SIZE)) {
        // normal case
        current = prev->next;
    } else {
        // the case when we can allocate in the very first element of the list of free blocks
        current = first_free;
        prev = NULL;
    }

    int old_free_size = current->size;
    free_block_t old_free_pointer = current->next;
    busy_block_t new_busy = (busy_block_t) current;
    new_busy->size = size;

    if (old_free_size >= size + sizeof(busy_block_s) + sizeof(free_block_s)) {
        // there is enough space to put free_block_s after our busy_block

        free_block_t new_free = (free_block_t) (ULONG(new_busy) + ULONG(sizeof(busy_block_s) + size));
        // some point arithmetics to find a place for a new free_block

        new_free->next = old_free_pointer;
        new_free->size = old_free_size - size - sizeof(busy_block_s);
        if (prev == NULL) {
            // we allocate in the very first element of the list of free blocks
            first_free = new_free;
        } else {
            prev->next = new_free;
        }
    } else {
        // there is NOT enough space to put free_block_s after our busy_block
        size = old_free_size - sizeof(busy_block_s);
        new_busy->size = size;
        if (prev == NULL) {
            // we allocate in the very first element of the list of free blocks
            if (first_free->next != NULL) {
                first_free = first_free->next;
            } else {
                // we don't have more memory
                first_free = NULL;
            }
        } else {
            prev->next = old_free_pointer;
        }
    }

	char *addr = (new_busy != NULL ? (char*) ULONG(new_busy) + ULONG(sizeof(busy_block_s)) : NULL);
    print_alloc_info(addr, size);
	return addr;
}