Meeting Summary: CS 326 Lecture/Lab Spring 2025
Date: Feb 11, 2025, 03:09 PM Pacific Time (US and Canada)
Meeting ID: 813 3845 7711
Quick Recap
Memory Layout and Allocation:
Discussed the process memory layout including allocation in 4kB chunks, the use of guard pages, heap management, and free block compaction.Repository Structure and Optimization:
Emphasized the importance of proper file names and directory organization—avoiding unwanted files like .o files. Discussed how compiler optimizations and makefile flags impact function ordering and code placement in memory, including the significance of the ELF format in Linux.Memory Allocator Implementation:
Introduced a new kernel memory allocator supporting memory allocation via system calls. Demonstrated the implementation of Malla Compute, a basic memory allocation system, and provided starter code for the upcoming memory allocator project.Exam Deadline:
Announced that exam questions will be due either Thursday or Friday. Clarified that students need only push their lab work to GitHub.
Next Steps
- Students:
- Review the starter code for the memory allocator project once it is available.
- Study the linked list implementation provided in the starter code.
- Complete and submit exam questions by Friday.
- Provide GitHub IDs (for Rachel, Colby, and Cal if not already submitted).
- Instructor (Greg):
- Distribute the starter code for the memory allocator project.
- Provide detailed explanations and diagrams on different memory scenarios in upcoming classes.
Detailed Summary
1. Memory Layout and Allocation Discussion
Greg explained the memory layout in a user process and highlighted the following concepts:
Memory Allocation:
Memory is allocated in 4kB chunks to match the processor and operating system expectations.Guard Pages:
A guard page is used to prevent accidental memory access or overflow.Heap Management:
The heap is managed by the memory allocator, with free blocks being merged (compaction) to optimize available memory.Garbage Collection:
While the conversation mentioned garbage collection, it was noted that forcing garbage collection is more common in languages like Python than in C.
2. File Names, Directories, and Optimization
Key points discussed include:
Repository Organization:
The importance of clean file naming and directory structuring in a repository was emphasized.
Avoid including files like.o
files as these are generated during compilation.Compiler Optimization:
Changing optimization flags in the makefile can affect both the code’s performance and the memory order of functions.ELF Format:
Understanding the ELF (Executable and Linkable Format) is essential since it dictates how a program is loaded into memory by the kernel.
Note: The order of functions in the source code is not important; the linker determines their final placement.
3. Code Analysis and Function Ordering
Points covered in this section include:
Code Analysis Tools:
Greg mentioned the use of paid services for code analysis, though the results were not as expected.- Function Ordering:
Observations during compilation indicated that:- Leaf functions appear first,
- Followed by non-leaf functions,
- And finally, the main function.
- Development Tools:
Discussion of using tools like VS Code for code formatting and searching, as well as micro scripting with potential integration issues.
4. Memory Allocator Implementation and Testing
The discussion continued with the specifics of the kernel memory allocator implementation:
- Memory Allocation & Deallocation:
- A positive number passed to the system call requests allocation.
- A negative number requests deallocation.
However, not all allocated memory might be deallocated due to inherent limitations in the allocator and OS memory management.
- Memory Testing:
Minor kernel modifications supported a memory test program that exercises the allocator.
Memory Test Code Example
This example demonstrates basic allocation and deallocation testing:
int main(void) {
// Attempt to allocate a 64-byte block
void *ptr = malloc(64);
if (ptr == 0) {
printf("Allocation failed\n");
return -1;
}
printf("Memory allocated at %p\n", ptr);
free(ptr);
return 0;
}
5. Linked List and Memory Allocation
Greg explained how the linked list is used to manage memory blocks within the allocator:
Linked List Structure:
The base shell for the memory allocator project includes a linked list implementation that tracks free and allocated blocks.Virtual Memory:
Each process has its own virtual address space, with parts mapped to physical memory.
Linked List Implementation Snippet
A simplified linked list structure used in memory allocators:
struct block {
size_t size;
int free;
struct block *next;
};
struct block *free_list = NULL;
6. Linked Lists Explained
A basic memory allocation system:
Malloc Overview:
Malla Compute allocates memory based on the number of requested bytes and returns a pointer to the beginning of the allocated memory block.Safe Experimentation:
Demonstrated within a virtual machine to ensure the system remains unaffected by experimental code.Linked Lists in Practice:
Provided an explanation of how any structure can be integrated into a linked list, emphasizing the flexibility of this data structure in memory management.
7. Memory Allocator Project and Exams
The session concluded with important project and exam announcements:
- Memory Allocator Project:
- Starter code and a block header structure (for both used and free blocks) will be provided.
- Students will implement the allocation algorithms and manage the merging of adjacent free blocks.
Action Items
- For Students:
- Review and study the starter code for the memory allocator project.
- Familiarize themselves with the linked list implementation.
- Complete and submit the exam questions by the specified due date.
- Submit GitHub IDs if not already provided (specifically for Rachel, Colby, and Cal).
- For Instructor (Greg):
- Distribute the starter code for the memory allocator project.
- Provide further detailed explanations and additional diagrams for memory scenarios in future classes.
Upcoming Topics
- In-depth diagrams and explanations of various memory scenarios.
- Further exploration of code formatting techniques and function ordering.
- Expanded discussion on memory management concepts and practical testing of the memory allocator.
References
- ELF Format:
- Free Block Compaction:
- Garbage Collection:
- Heap Management: