Task7498

From GNUpdf

Task #7498
Implementation of the Memory Allocation module
State TODO
Priority 5
Estimated effort 4ph
Scheduled 29-11-2007
Deadline 29-11-2007
Start
End
Assigned to
Savannah task

Contents

Implementation of the Memory Allocation module

Overview

The memory allocation module provides system-independent heap memory allocation and deallocation. The usual malloc/free/realloc schema is used to provide this service.


Description

The module should support the following API.

Function: char* pdf_alloc (int size)

Allocates heap memory.

Parameters
size

The requested number of octects to allocate.

If there is not enough available memory to satisfy the petition a fatal error is signaled killing the current process. An error status is returned to the operating system.


Returns

A pointer to the newly allocated memory.

Usage example

 
int *p;

p = (int *) pdf_alloc (sizeof(int));
p = 666;

Function: void pdf_dealloc (char *pointer)

Deallocates heap memory.

Parameters
pointer

A pointer pointing to the memory we want to deallocate. The memory to deallocate should have been previously allocated using pdf_alloc.


Returns

None.

Usage Example

 
char *p;

p = (char *) pdf_alloc (21);
pdf_dealloc (p);

Function: char* pdf_realloc (char *pointer, int size)

Reallocates memory.

Parameters
pointer

A pointer to previously allocated memory.

The memory to reallocate should have been allocated using pdf_alloc or pdf_realloc.

size

The new size of the allocated memory chunk.

If the requested size is shorter than the original size of the allocated memory then it is truncated. Any previous contents in the memory will be lost.

If the requested size is larger or equal than the original size of the allocated memory then the previous contents of the allocated memory remains. The contents of newly allocated memory are undetermined.

If there is not enough available memory to satisfy the petition a fatal error is signaled killing the current process. An error status is returned to the operating system.


Returns

A pointer to the reallocated memory.

Usage Example

 
char *p;

p = (char *) pdf_alloc (4);
strncpy (p, "abcd", 4);

/* p now points to "abcd" */

p = (char *) pdf_realloc (5);
p[4] = 'e';

/* p now points to "abcde" */

p = (char *) pdf_realloc (4);

/* p now points to "abcd" */

pdf_dealloc (p);

References

Retrieved from "http://gnupdf.org/Task7498"