Skip to content
Snippets Groups Projects
Select Git revision
  • 7dec327995d98ba151b9c0153713bcdcd9aea3a3
  • 3.9 default
  • develop
  • 6.0
  • 5.0
  • 4.0
  • scrutinizer-patch-4
  • scrutinizer-patch-3
  • scrutinizer-patch-2
  • scrutinizer-patch-1
  • 3.7
  • 3.8
  • 3.6
  • 3.9_backported
  • 3.8_backported
  • 3.7_backported
  • 3.5
  • 3.6_backported
  • 3.5_backported
  • 3.4
  • 3.3_backported
  • 6.0.4
  • 6.0.3
  • 5.0.7
  • 6.0.2
  • 6.0.1
  • 5.0.6
  • 6.0.0
  • 5.0.5
  • 6.0.0-rc
  • 5.0.4
  • 6.0.0-beta
  • 5.0.3
  • 4.0.6
  • 5.0.2
  • 5.0.1
  • 4.0.5
  • 5.0.0
  • 4.0.4
  • 5.0.0-rc2
  • 5.0.0-rc1
41 results

functions.sql

Blame
  • memlib.c 2.22 KiB
    /*
     * memlib.c - a module that simulates the memory system.  Needed because it 
     *            allows us to interleave calls from the student's malloc package 
     *            with the system's malloc package in libc.
     */
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <unistd.h>
    #include <sys/mman.h>
    #include <string.h>
    #include <errno.h>
    
    #include "memlib.h"
    #include "config.h"
    
    /* private variables */
    static char *mem_start_brk;  /* points to first byte of heap */
    static char *mem_brk;        /* points to last byte of heap */
    static char *mem_max_addr;   /* largest legal heap address */ 
    
    /* 
     * mem_init - initialize the memory system model
     */
    void mem_init(void)
    {
        /* allocate the storage we will use to model the available VM */
        if ((mem_start_brk = (char *)malloc(MAX_HEAP)) == NULL) {
    	fprintf(stderr, "mem_init_vm: malloc error\n");
    	exit(1);
        }
    
        mem_max_addr = mem_start_brk + MAX_HEAP;  /* max legal heap address */
        mem_brk = mem_start_brk;                  /* heap is empty initially */
    }
    
    /* 
     * mem_deinit - free the storage used by the memory system model
     */
    void mem_deinit(void)
    {
        free(mem_start_brk);
    }
    
    /*
     * mem_reset_brk - reset the simulated brk pointer to make an empty heap
     */
    void mem_reset_brk()
    {
        mem_brk = mem_start_brk;
    }
    
    /* 
     * mem_sbrk - simple model of the sbrk function. Extends the heap 
     *    by incr bytes and returns the start address of the new area. In
     *    this model, the heap cannot be shrunk.
     */
    void *mem_sbrk(int incr) 
    {
        char *old_brk = mem_brk;
    
        if ( (incr < 0) || ((mem_brk + incr) > mem_max_addr)) {
    	errno = ENOMEM;
    	fprintf(stderr, "ERROR: mem_sbrk failed. Ran out of memory...\n");
    	return (void *)-1;
        }
        mem_brk += incr;
        return (void *)old_brk;
    }