Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: mem_pool_manager.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script simulates a memory pool manager in Python, demonstrating basic memory allocation and deallocation processes.
- - Although Python has built-in memory allocation and garbage collection mechanisms, this script serves as an educational example of how memory allocation management can be implemented in a programming language.
- - It provides functions to allocate and deallocate memory blocks of various sizes within a simulated memory pool environment.
- - The MemoryPool class allows users to allocate and deallocate memory blocks, while the MemoryBlock class represents individual blocks of memory with specific sizes.
- - This script is designed to be a learning tool for understanding memory management concepts and is not intended for real-world memory management tasks.
- Requirements:
- - Python 3.x
- Functions:
- - MemoryBlock:
- Represents a block of memory with a specific size.
- - MemoryPool:
- Represents a pool of allocated memory blocks.
- - allocate(size):
- Allocate a new memory block with the specified size.
- - deallocate(size):
- Deallocate a memory block with the specified size.
- - print_blocks():
- Print the current state of allocated memory blocks.
- Usage:
- - Run the script to allocate and deallocate memory blocks of various sizes.
- - Modify the script to customize memory allocation and deallocation behavior.
- Additional Notes:
- - This script is for educational purposes and simulates basic memory management concepts.
- - It does not simulate actual system memory allocation and deallocation processes.
- """
- class MemoryBlock:
- """Represents a block of memory with a specific size."""
- def __init__(self, size):
- """Initialize a memory block with a specific size."""
- self.size = size
- class MemoryPool:
- """Represents a pool of allocated memory blocks."""
- def __init__(self):
- """Initialize the memory pool as an empty list of memory blocks."""
- self.blocks = []
- def allocate(self, size):
- """Allocate a new memory block with the specified size."""
- new_block = MemoryBlock(size)
- self.blocks.append(new_block)
- print(f"\tAllocated memory block of size {size}")
- def deallocate(self, size):
- """Deallocate a memory block with the specified size."""
- self.blocks = [block for block in self.blocks if block.size != size]
- print("-" * 50)
- print(f"\n- Deallocated memory block(s) of size {size}\n")
- print("-" * 50)
- def print_blocks(self):
- """Print the current state of allocated memory blocks."""
- print("Current allocated memory blocks:\n")
- for block in self.blocks:
- print(f"\tBlock size: {block.size}")
- # Usage example
- def main():
- print("-" * 50)
- print("- Allocating memory block sizes...")
- print("-" * 50)
- memory_pool = MemoryPool()
- memory_pool.allocate(64)
- memory_pool.allocate(128)
- memory_pool.allocate(256)
- print("-" * 50)
- memory_pool.print_blocks()
- memory_pool.deallocate(128)
- memory_pool.print_blocks()
- print("-" * 50)
- print("\tMemory Block Testing Complete!\n\n\tExiting Program... GoodBye!")
- print("-" * 50)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement