Memory Management - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Memory Management

Description:

The 'Big' Picture. Swap. UFS. app1. Disk. DRAM. CPU. Unitialized data. Allocated ... it first allocates memory from the free store, for example using malloc ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 14
Provided by: Fre58
Category:

less

Transcript and Presenter's Notes

Title: Memory Management


1
Memory Management
  • Fred Kuhns
  • fredk_at_cse.wustl.edu
  • Applied Research Laboratory,
  • Department of Computer Science and Engineering,
  • Washington University in St. Louis

2
The Big Picture
CPU
Unitialized data
Stack and heap
DRAM
Allocated virtual pages
Low Address (0x00000000)
Swap
app1 Address space
Disk
UFS
High Address (0x7fffffff)
Text and initialized data
app1
3
Your Programs Address Space
Codeint main() void fn() MyClassdoit()
Low Address (0x00000000)
Global int x 4
Global int y
Process Address space
Global dynamically allocated (allocate with
new/malloc)
Local variables, example, function calls
stack (dynamic)
High Address (0x7fffffff)
4
Buddy System Example Memory Allocator
Free list
32 64 128 256 512
0
1023
256
384
448
512
640
768
B
C
D
D
F
F
E
Bitmap (32 B chunks)
Free
In-use
allocate(256), allocate(128), allocate(64),
allocate(128), release(C, 128), release (D, 64)
1
5
C-Style memory allocation - alloc
  • malloc() and related functions
  • void malloc(size_t)
  • void calloc(size_t, size_t)
  • void free(void )
  • Allocate memory from the heap
  • implementation will request memory pages (usually
    4KB) from OS then divide this into allocation
    blocks
  • for example a simple buddy-system allocator

Text (shared)
Data
Heap (Dynamic)
malloc(b_bytes)
obj-gtdoit()
obj1-gtsomething()
main() locals
stack (dynamic)
MyStruct m (MyStruct )malloc(sizeof(MyStruct))

6
C-Style memory allocation - free
  • when free() is called the memory is returned to
    the free store and is eligible to be re-allocated.

Text (shared)
Data
Heap (Dynamic)
free(b_bytes)
obj-gtdoit()
obj1-gtsomething()
main() locals
stack (dynamic)
free(m)
7
C Style (an example using malloc/free)
  • new/delete operators
  • when the compiler encounters a new operator
  • it first allocates memory from the free store,
    for example using malloc()
  • second it calls the object constructor
  • when compiler encounters the delete operator it
  • call the object destructor
  • it returns memory to the free store, for example
    using free()

Text (shared)
Data
Heap (Dynamic)
data for object MyClass
malloc(b_bytes)
obj-gtdoit()
obj1-gtsomething()
main() locals
Psuedo C simulating new MyClass void m
malloc(sizeof(MyClass)) MyClassMyClass(thism)
stack (dynamic)
8
C Style (an example using malloc/free)
  • when delete is called ,the compiler will first
    call the objects destructor before freeing the
    memory (i.e. return memory to the free store)

Text (shared)
Data
Heap (Dynamic)
free(b_bytes)
obj-gtdoit()
obj1-gtsomething()
main() locals
Psuedo C simulating delete m MyClassMyClass(t
hism) free(m)
stack (dynamic)
9
Operator new and delete
  • Any class may override these operators. They are
    implicitly static members.
  • Derived classes necessarily inherit these
    operators
  • Operator new() always has at least one argument
    (implicit), the number of bytes to allocate
  • void operator new (size_t)
  • Operator delete() has two arguments, the objects
    address and its size in bytes.
  • void delete(void, size_t)
  • For the array versions the size argument is for
    the total number of bytes to allocate
  • void operator new(size_t)
  • void operator delete(void, size_t)

10
An Example
  • class Base
  • public
  • int i
  • Base() i(0)
  • virtual Base()
  • // objects
  • void operator new (size_t n) return (void
    )malloc(n)
  • void operator delete(void p, size_t n)
    free(p)return
  • // Arrays
  • void operator new (size_t n) return (void
    )malloc(n)
  • void operator delete (void p, size_t
    n)free(p) return
  • class Derived public Base
  • public
  • int b
  • Derived() b(0)
  • Derived()

11
Observations
  • The base class must declare its destructor to be
    virtual. Why? What happens if it doesnt
  • The C environment keeps track of the objects
    size when calling delete hint for the answer
    above
  • we should throw bad_alloc() if unable to allocate
    memory

12
Operator new and exceptions
  • operator new throws bad_alloc if it is unable to
    allocate memory
  • void operator new(size_t size)
  • for ()
  • if (void p malloc(size)) return p
  • if (_new_handler 0) throw bad_alloc()
  • _new_handler()
  • You can set the new_handler with a call to
    set_new_handler() declared in ltnewgt.
  • effective strategy is to attempt freeing
    resources but if unable then either throw
    bad_alloc() yourself or remove your new_handler
    to operator new will.

13
placement syntax
  • You can supply extra arguments to new
  • void operator new(size_t, void p) return p
  • void buf reinterpret_castltvoid gt(0xfoof)
  • X p2new(buf)X
  • The call to new will return the address buf
  • class Arena
  • public
  • virtual void alloc(size_t) 0
  • virtual void free(virtual) 0
  • void operator new(size_t n, Arena a)
  • return a-gtalloc(n)
  • X ptr new(MyArena) X()
  • The delete operator can also be overloaded but it
    will only be called implicitly if the
    corresponding new operator throws an exception.
    Otherwise delete is not called. The best strategy
    is to define a destroy() function that calls the
    destructor() and free() methods explicitly.
  • void destroy(X p, Arean a)
  • p-gtX()
  • a-gtfree(p)
Write a Comment
User Comments (0)
About PowerShow.com