描述:
There is little time left before the release of the first national operating system BerlOS. Some of its components are not finished yet — the memory manager is among them. According to the developers' plan, in the first release the memory manager will be very simple and rectilinear. It will support three operations:
在国家第一个操作系统BerIOS出现之前有很少的时间。它的某些组件未完成——内存管理器就是其中之一。根据设计者的计划,在发行的第一版的内存管理器将会非常简单和直接。他将支持3个操作。
- alloc n — to allocate n bytes of the memory and return the allocated block's identifier x;
- erase x — to erase the block with the identifier x;
- defragment — to defragment the free memory, bringing all the blocks as close to the beginning of the memory as possible and preserving their respective order;
- alloc n——分配内存的n个字节并且返回分配符的标识符x。
- erase x——清除标识符为x的块。
- 碎片整理 — 对可用内存进行碎片整理,使所有块尽可能接近内存的开头并保持其各自的顺序;
The memory model in this case is very simple. It is a sequence of m bytes, numbered for convenience from the first to the m-th.
The first operation alloc n takes as the only parameter the size of the memory block that is to be allocated. While processing this operation, a free block of n successive bytes is being allocated in the memory. If the amount of such blocks is more than one, the block closest to the beginning of the memory (i.e. to the first byte) is prefered. All these bytes are marked as not free, and the memory manager returns a 32-bit integer numerical token that is the identifier of this block. If it is impossible to allocate a free block of this size, the function returns NULL.
The second operation erase x takes as its parameter the identifier of some block. This operation frees the system memory, marking the bytes of this block as free for further use. In the case when this identifier does not point to the previously allocated block, which has not been erased yet, the function returns ILLEGAL_ERASE_ARGUMENT.
The last operation defragment does not have any arguments and simply brings the occupied memory sections closer to the beginning of the memory without changing their respective order.
In the current implementation you are to use successive integers, starting with 1, as identifiers. Each successful alloc operation procession should return following number. Unsuccessful alloc operations do not affect numeration.
You are to write the implementation of the memory manager. You should output the returned value for each alloc command. You should also output ILLEGAL_ERASE_ARGUMENT for all the failed erase commands.
在这种情况下,内存模型非常简单。它是一个 m 字节序列,为方便起见,从第一个字节到第 m 个字节进行编号。
第一个操作 alloc n 将要分配的内存块的大小作为唯一参数。在处理此操作时,将在内存中分配 n 个连续字节的可用块。如果此类块的数量超过一个,则首选最接近内存开头(即第一个字节)的块。所有这些字节都被标记为非空闲,内存管理器返回一个 32 位整数数字标记,该标记是此块的标识符。如果无法分配此大小的可用块,则该函数将返回 NULL。
第二个操作擦除 x 将某个块的标识符作为其参数。此操作将释放系统内存,将此块的字节标记为可用以供进一步使用。如果此标识符未指向以前分配的块(该块尚未擦除),则该函数将返回ILLEGAL_ERASE_ARGUMENT。
最后一个操作碎片整理没有任何参数,只是使占用的内存部分更接近内存的开头,而不更改其各自的顺序。
在当前实现中,您将使用从 1 开始的连续整数作为标识符。每个成功的 allose 操作过程都应返回以下编号。不成功的分配操作不会影响计数。
您将编写内存管理器的实现。您应该输出每个 alloc 命令的返回值。您还应该为所有失败的擦除命令输出ILLEGAL_ERASE_ARGUMENT。
输入:
The first line of the input data contains two positive integers t and m (1 ≤ t ≤ 100;1 ≤ m ≤ 100), where t — the amount of operations given to the memory manager for processing, and m — the available memory size in bytes. Then there follow t lines where the operations themselves are given. The first operation is alloc n (1 ≤ n ≤ 100), where n is an integer. The second one is erase x, where x is an arbitrary 32-bit integer numerical token. The third operation is defragment.
输入数据的第一行包含两个正整数 t 和 m(1 ≤ t ≤ 100;1 ≤ m ≤ 100),其中 t — 提供给内存管理器进行处理的操作量,m — 可用内存大小(以字节为单位)。然后,沿着 t 行给出操作本身。第一个运算是 alloc n (1 ≤ n ≤ 100),其中 n 是整数。第二个是 erase x,其中 x 是任意的 32 位整数数字标记。第三个操作是碎片整理。
输出:
Output the sequence of lines. Each line should contain either the result of alloc operation procession , or ILLEGAL_ERASE_ARGUMENT as a result of failed erase operation procession. Output lines should go in the same order in which the operations are processed. Successful procession of alloc operation should return integers, starting with 1, as the identifiers of the allocated blocks.
输出行序列。每行应包含 allose 操作处理的结果,或ILLEGAL_ERASE_ARGUMENT擦除操作处理失败的结果。输出行应按处理操作的相同顺序排列。成功的 alloc 操作处理应返回以 1 开头的整数作为已分配块的标识符。
样例输入:
6 10 alloc 5 alloc 3 erase 1 alloc 6 defragment alloc 6
样例输出:
1 2 NULL 3
本文档描述了一个简单的内存管理器的设计,该管理器用于国家首个操作系统BerIOS的初步版本。内存管理器支持三个基本操作:分配内存、清除内存块和碎片整理。分配操作在内存中寻找连续的自由块,若找到则返回一个唯一的32位标识符,否则返回NULL。清除操作接受一个块的标识符,无效标识符会导致错误。碎片整理操作将内存中的占用部分紧凑到内存开头。输入包括一系列操作,输出是对每个操作的结果,成功的分配操作返回从1开始的整数标识符,清除操作失败则返回ILLEGAL_ERASE_ARGUMENT。

1726

被折叠的 条评论
为什么被折叠?



