Advertisement
Python253

mem_pool_manager

May 24th, 2024
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: mem_pool_manager.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script simulates a memory pool manager in Python, demonstrating basic memory allocation and deallocation processes.
  10.    - 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.
  11.    - It provides functions to allocate and deallocate memory blocks of various sizes within a simulated memory pool environment.
  12.    - The MemoryPool class allows users to allocate and deallocate memory blocks, while the MemoryBlock class represents individual blocks of memory with specific sizes.
  13.    - This script is designed to be a learning tool for understanding memory management concepts and is not intended for real-world memory management tasks.
  14.    
  15. Requirements:
  16.    - Python 3.x
  17.  
  18. Functions:
  19.    - MemoryBlock:
  20.        Represents a block of memory with a specific size.
  21.    - MemoryPool:
  22.        Represents a pool of allocated memory blocks.
  23.        - allocate(size):
  24.            Allocate a new memory block with the specified size.
  25.        - deallocate(size):
  26.            Deallocate a memory block with the specified size.
  27.        - print_blocks():
  28.            Print the current state of allocated memory blocks.
  29.  
  30. Usage:
  31.    - Run the script to allocate and deallocate memory blocks of various sizes.
  32.    - Modify the script to customize memory allocation and deallocation behavior.
  33.  
  34. Additional Notes:
  35.    - This script is for educational purposes and simulates basic memory management concepts.
  36.    - It does not simulate actual system memory allocation and deallocation processes.
  37. """
  38.  
  39. class MemoryBlock:
  40.     """Represents a block of memory with a specific size."""
  41.    
  42.     def __init__(self, size):
  43.         """Initialize a memory block with a specific size."""
  44.         self.size = size
  45.  
  46. class MemoryPool:
  47.     """Represents a pool of allocated memory blocks."""
  48.    
  49.     def __init__(self):
  50.         """Initialize the memory pool as an empty list of memory blocks."""
  51.         self.blocks = []
  52.  
  53.     def allocate(self, size):
  54.         """Allocate a new memory block with the specified size."""
  55.         new_block = MemoryBlock(size)
  56.         self.blocks.append(new_block)
  57.         print(f"\tAllocated memory block of size {size}")
  58.  
  59.     def deallocate(self, size):
  60.         """Deallocate a memory block with the specified size."""
  61.         self.blocks = [block for block in self.blocks if block.size != size]
  62.         print("-" * 50)
  63.         print(f"\n- Deallocated memory block(s) of size {size}\n")
  64.         print("-" * 50)
  65.    
  66.     def print_blocks(self):
  67.         """Print the current state of allocated memory blocks."""
  68.         print("Current allocated memory blocks:\n")
  69.         for block in self.blocks:
  70.             print(f"\tBlock size: {block.size}")
  71.  
  72. # Usage example
  73. def main():
  74.     print("-" * 50)
  75.     print("- Allocating memory block sizes...")
  76.     print("-" * 50)
  77.    
  78.     memory_pool = MemoryPool()
  79.  
  80.     memory_pool.allocate(64)
  81.     memory_pool.allocate(128)
  82.     memory_pool.allocate(256)
  83.     print("-" * 50)
  84.    
  85.     memory_pool.print_blocks()
  86.  
  87.     memory_pool.deallocate(128)
  88.  
  89.     memory_pool.print_blocks()
  90.    
  91.     print("-" * 50)
  92.     print("\tMemory Block Testing Complete!\n\n\tExiting Program...   GoodBye!")
  93.     print("-" * 50)
  94.    
  95. if __name__ == "__main__":
  96.     main()
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement