Monthly Archive for March, 2007

ERROR: mem_sbrk failed. Ran out of memory…

mdriver prints the ERROR: mem_sbrk failed. Ran out of memory... error message when the simulated memory heap (20MB) is full. Some of the test traces allocate a total of 37MB (without considering frees). This could therefore happen if:

  • your free routine fails to free the released blocks
  • your malloc routine fails to use freed blocks
  • your allocation algorithm generates too much internal fragmentation

Deadline for the malloc lab

The correct deadline for the malloc lab is April 8.

Structs and field alignment

Compilers usually align fields to allow faster access. It is usually possible to tell the compiler to avoid this behavior and generate structs as they are specified. gcc uses the attribute tag. For example the following code

struct test_t {
  char  b;
  long  l;
};

struct test_t test = { 'a', 42 };

is compiled (on an Intel architecture) as

.globl _test
    .data
    .align 2
_test:
    .byte   97
    .space 3
    .long   42
    .subsections_via_symbols

where three bytes are used to pad the char to 32 bits. While

struct test_t {
  char  b;
  long  l;
} __attribute__ ((packed));
struct test_t test = { 'a', 42 } ;

is compiled as

.globl _test
    .data
_test:
    .byte   97
    .long   42
    .subsections_via_symbols

Byte aligment

The macros to align the addresses returned by malloc in the given example (in malloc-handout) are wrong since they only deliver 8-byte aligned results.

The results must of course be and-ed with ~(ALIGNMENT-1) and not ~0x7 (which is only OK if ALIGNMENT is 8).

Please patch your mm.c file:

#define ALIGNMENT 8
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~(ALIGNMENT-1))
#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))

A new tarball (malloclab-handout.tar) is available