示例#1
0
文件: malloc.c 项目: devansc/OS
/*
 * Attempts to reallocate an AllocUnit au. Attempts in place expansion, and 
 * if not, merges with neighboring free AllocUnits if possible. Then if that
 * isn't enough size, allocates a new AllocUnit with the correct size.
 */
AllocUnit *reallocate(AllocUnit *au, size_t size) {
    AllocUnit *newAU;
    size_t origSize = size;
    if (au->size >= size) {
        au->size = size;
        au->isFree = 0;
        return au;
    }

    /* Merges with next AU if free */
    if (au->next != NULL && au->next->isFree) {
        mergeAU(au);
    }
    /* Merges with last AU if free */
    if (au->last != NULL && au->last->isFree) {
        mergeAU(au->last);
    }

    if (au->last != NULL && au->last->isFree && au->last->size >= size) {
        newAU = au->last;
        newAU = unfreeAU(au->last, size);
        if (newAU != NULL) 
            memmove(newAU->memLoc, au->memLoc, origSize);
        return newAU;
    } else if (au->size >= size) {
        au = unfreeAU(au, size);
        return au; 
    } else {
        newAU = allocateNew(size);
        if (newAU != NULL) 
            memmove(newAU->memLoc, au->memLoc, origSize);
        return newAU;
    }
}
示例#2
0
文件: malloc.c 项目: devansc/OS
/*
 * Implementation of stdlib malloc.
 */
void * malloc(size_t size) {
    AllocUnit *au = allocateNew(size);
    if (au == NULL) {
        errno = ENOMEM;
        return NULL;
    }
    if (debugMalloc()) {
        printf("MALLOC: malloc(%zu)        =>   (ptr=%p, size=%zu)\n", size,
                au->memLoc,au->size); 
    }
    return au->memLoc;
}
示例#3
0
//method for allocating memory for memory block
void* getmem(uintptr_t size){
	if(size<=0){
		return NULL;
	}
	uintptr_t actual_size = (size % HEAD_SIZE != 0) ? (size+HEAD_SIZE) + (HEAD_SIZE - (size % HEAD_SIZE)) : (size+HEAD_SIZE); 

	if(free_list == NULL){
		
		return allocateNew(actual_size);
	}

	memnode* cur = free_list;

	// first add header(16 bytes) and then make it a mutiple of 16

	while(cur != NULL){

		if(cur->size > actual_size){ //check if there is enough block for new request

			memnode* newMem = (memnode*)((uintptr_t)cur + HEAD_SIZE + cur->size - actual_size);
			
			newMem->size = actual_size - HEAD_SIZE;
			newMem->next = NULL;
			cur->size = cur->size - actual_size;

			add_to_allocated_list(newMem);
			return (void*) ((memnode*) ((uintptr_t)newMem+HEAD_SIZE));
		}else if(cur->size == actual_size){
			//delete cur from free_list and add it to the AMA
			delete_from_free(cur);
			add_to_allocated_list(cur);

			return (void*) ((memnode*) ((uintptr_t)cur+HEAD_SIZE));
		}
		cur = cur->next;
	}
	return allocateNew(actual_size);
}
示例#4
0
文件: malloc.c 项目: devansc/OS
/*
 * Implementation of stdlib calloc.
 */
void * calloc(size_t nmemb, size_t size) {
    int paddedSize;
    AllocUnit *au;
    paddedSize  = padSize(nmemb * size);
    au = allocateNew(paddedSize);
    if (au == NULL) {
        errno = ENOMEM;
        return NULL;
    }
    memset(au->memLoc, 0, paddedSize);
    if (debugMalloc()) {
        printf("MALLOC: calloc(%zu,%zu)     =>   (ptr=%p, size=%zu)\n",nmemb,
                size,au->memLoc,au->size); 
    }
    return au->memLoc;
}
/// Resize packet in FIFO buffer.
Packet_t *Packet_Resize(Packet_t *packet, uint16_t len)
{
    uint16_t oldLen = packet->state;
    assert((oldLen & Skip) == 0);

    Packet_t *nextPacket = getNextPacket(packet, len);
    Packet_t *oldNextPacket = getNextPacket(packet, oldLen);

    // Packet shrink, need to do something with remainder
    if(oldNextPacket > nextPacket)
    {
        if(NextWriter == oldNextPacket)	// Last element in ring, adjust next write position
            NextWriter = nextPacket;
        else				// Otherwise mark remainder as Skip
            nextPacket->state = Skip | ((uintptr_t)oldNextPacket - (uintptr_t)nextPacket->data);
    }

    // Packet shrink or same size
    if(oldNextPacket >= nextPacket)
    {
        // even if oldNextPacket == nextPacket it's possible that len != oldLen
        packet->state = len;
        return packet;
    }

    // Packet extend
    Packet_t *lastReader = OutputReader;
    Packet_t *writer = NextWriter;

    // Check if packet is last element in RingBuffer and we have enough space to append the extra bytes
    if(oldNextPacket == writer && ((packet < lastReader) ? (nextPacket < lastReader) : (nextPacket <= RingBuffer.End)))
    {
        nextPacket->state = EndOfRing;
        packet->state = len;
        NextWriter = nextPacket;
        return packet;
    }

    // We cannot append, create new packet of full length
    return allocateNew(writer, lastReader, len, true, packet, oldLen);
}
/// Allocate memory for a new packet in FIFO buffer.
Packet_t *Packet_New(uint16_t len)
{
    return allocateNew(NextWriter, OutputReader, len, false, NULL, 0);
}