Memory allocation.

Slides:



Advertisements
Similar presentations
Process A process is usually defined as an instance of a running program and consists of two components: A kernel object that the operating system uses.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
KERNEL MEMORY ALLOCATION Unix Internals, Uresh Vahalia Sowmya Ponugoti CMSC 691X.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
User-Level Memory Management in Linux Programming
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Win32 Programming Lesson 5: Error Codes. Before We Begin  Much of the time in this class we’ll be calling Win32 Functions  However, sometimes they’re.
1 JMH Associates © 2004, All rights reserved Chapter 5 Memory Management, Memory-Mapped Files, and DLLs.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
Windows Memory Management, Memory- Mapped Files, and DLLs.
Memory management under Windows The Need for Memory:-  The window environment and most applications that run under it need access to phenomenal amounts.
Win32 Programming Lesson 16: Virtual Memory. Where are we?  We’ve covered the theory of Windows memory, and poked around some  Now let’s use how to.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Lecture 13 Static vs Dynamic Memory Allocation
Win32 Programming Lesson 18: More Memory Mapped Files and the HEAP (Finally, cool stuff!)
Windows Memory Architecture 井民全製作. A Process ’ s Virtual Address Space Every Process has its own private virtual address 32-bits processes  4 GB address.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Memory Management II CS Spring Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address.
Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)
CNIT 127: Exploit Development Ch 8: Windows Overflows Part 2.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Processes and Virtual Memory
Win32 Programming Lesson 15: Practical Windows Memory (If you can read this you have good vision)
“WALK IN” SLIDE. August Memory Management Internals Steve Smith Software Design Engineer Game Technology Group Microsoft Presentation/Presenter.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
Fig Storage of a C program. Fig Memory allocation with malloc.
Learners Support Publications Constructors and Destructors.
Memory Mapped I/O Gregory Mortensen CSIS 4330, Advanced Windows Programming – UVSC.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Constructors and Destructors
OPERATING SYSTEM CONCEPT AND PRACTISE
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Interface Definition Language
Konstantin Bukin CSE791 – Advanced Windows Programming Summer 2001
Understand Computer Storage and Data Types
Programmazione I a.a. 2017/2018.
CSCI206 - Computer Organization & Programming
This pointer, Dynamic memory allocation, Constructors and Destructor
Data Structures and Abstract Data Types
Programming and Data Structures
Windows CE Memory Management
Memory Segments Code (.code) Data (.data) Stack Heap
Pointers, Dynamic Data, and Reference Types
Further Data Structures
Memory Allocation CS 217.
Operators.
Constructors and Destructors
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Chapter 10-1: Dynamic Memory Allocation
Areas Of Focus Image format Firmware Memory management Problem areas
Buddy Allocation CS 161: Lecture 5 2/11/19.
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/22.
COMP755 Advanced Operating Systems
Pointers, Dynamic Data, and Reference Types
Module 13 Dynamic Memory.
Dynamic Data Structures
Presentation transcript:

Memory allocation

Methods CoTaskMemAlloc GlobalAlloc HeapAlloc LocalAlloc malloc new VirtualAlloc

Comparison I HeapAlloc – can be instructed to raise an exception if memory could not be allocated LocalAlloc – supports allocation of handles which permit the underlying memory to be moved by reallocation without changing the handle value GlobalAlloc and LocalAlloc are wrappers for HeapAlloc HeapFree, GlobalFree, LocalFree – for releasing memory * GlobalAlloc, LocalAlloc and HeapAlloc functions allocates memory from the same heap. There was a difference in memory handling with 16-bit Windows ** Bold means not available for other functions *** Starting with 32-bit Windows GlobalAlloc and LocalAlloc are implemented as wrapper functions that call HeapAlloc

Comparison II VirtualAlloc – allows to specify additional options for memory allocation Can result in higher memory usage malloc – run-time dependent new – compiler and language dependant CoTaskMemAlloc – has advantage of working well in C, C++, VisualBasic The only way to share memory in a COM-based application CoTaskMemFree The virtual memory functions enable a process to manipulate or determine the status of pages in its virtual address space. This allocation use a page granularity Disadvantage to use malloc and new

CoTaskMemAlloc (Combaseapi.h) LPVOID CoTaskMemAlloc(SIZE_T cb); LPVOID CoTaskMemRealloc(LPVOID pv, SIZE_T cb); void CoTaskMemFree( _Frees_ptr_opt_ LPVOID pv); Allocates a block of task memory in the same way that IMalloc::Alloc does IMalloc – interface to manage memory (from Objidl.h) Alloc – allocates block of memory

Heap* (I) (Heapapi.h) HANDLE HeapCreate( DWORD flOptions, SIZE_T dwInitialSize, SIZE_T dwMaximumSize ); DECLSPEC_ALLOCATOR LPVOID HeapAlloc( HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes ); Each process has a default heap provided by the system. Applications that make frequent allocations from the heap can improve performance by using private heaps. The HeapCreate function creates a private heap object from which the calling process can allocate memory blocks by using the HeapAlloc function. Memory requested by HeapCreate may or may not be contiguous. Memory allocated within a heap by HeapAlloc is contiguous.

Heap* (II) DECLSPEC_ALLOCATOR LPVOID HeapReAlloc( HANDLE hHeap, DWORD dwFlags, _Frees_ptr_opt_ LPVOID lpMem, SIZE_T dwBytes );

Heap* (III) BOOL HeapFree( HANDLE hHeap, DWORD dwFlags, _Frees_ptr_opt_ LPVOID lpMem ); BOOL HeapDestroy( HANDLE hHeap );

GlobalAlloc _Frees_ptr_opt_ HGLOBAL hMem DECLSPEC_ALLOCATOR HGLOBAL GlobalAlloc( UINT uFlags, SIZE_T dwBytes ); HGLOBAL GlobalFree( _Frees_ptr_opt_ HGLOBAL hMem

LocalAlloc _Frees_ptr_opt_ HLOCAL hMem DECLSPEC_ALLOCATOR HLOCAL LocalAlloc( UINT uFlags, SIZE_T uBytes ); HLOCAL LocalFree( _Frees_ptr_opt_ HLOCAL hMem

VirtualAlloc LPVOID lpAddress, LPVOID VirtualAlloc( SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect ); BOOL VirtualFree( DWORD dwFreeType Reserves, commits, or changes the state of a region of pages in the virtual address space of the calling process. Memory allocated by this function is automatically initialized to zero.

VirtualAlloc2 HANDLE Process, PVOID VirtualAlloc2( PVOID BaseAddress, SIZE_T Size, ULONG AllocationType, ULONG PageProtection, MEM_EXTENDED_PARAMETER *ExtendedParameters, ULONG ParameterCount ); Reserves, commits, or changes the state of a region of memory within the virtual address space of a specified process ParameterCount - the number of extended parameters pointed to by ExtendedParameters.

ExtendedParameters typedef struct MEM_EXTENDED_PARAMETER { struct { DWORD64 Type : MEM_EXTENDED_PARAMETER_TYPE_BITS; DWORD64 Reserved : 64 - MEM_EXTENDED_PARAMETER_TYPE_BITS; } DUMMYSTRUCTNAME; union { DWORD64 ULong64; PVOID Pointer; SIZE_T Size; HANDLE Handle; DWORD ULong; } DUMMYUNIONNAME; } MEM_EXTENDED_PARAMETER, *PMEM_EXTENDED_PARAMETER; A pointer to one or more extended parameters of type MEM_EXTENDED_PARAMETER. You need to provide Type for this sort of parameter

Long pointer (LP*) to … LPTSTR name = NULL; DWORD dwAcctName = 1;  can be an integer name = (LPTSTR)GlobalAlloc( GMEM_FIXED, dwAcctName );