| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <unistd.h> |
| #include <time.h> |
|
|
| #define MAX_SLOTS 8 |
| #define SIZES_COUNT 3 |
|
|
| static int sizes[SIZES_COUNT] = {0x20, 0x40, 0x80}; |
|
|
| struct slot { |
| char *data; |
| int size_class; |
| int active; |
| }; |
|
|
| static struct slot slots[MAX_SLOTS]; |
|
|
| |
| static void heap_noise(void) { |
| srand(time(NULL) ^ getpid()); |
| int n = 3 + (rand() % 5); |
| for (int i = 0; i < n; i++) { |
| int sz = sizes[rand() % SIZES_COUNT]; |
| char *p = malloc(sz); |
| memset(p, 'N', sz); |
| if (rand() % 3 == 0) free(p); |
| } |
| } |
|
|
| int main(void) { |
| setbuf(stdin, NULL); |
| setbuf(stdout, NULL); |
|
|
| heap_noise(); |
|
|
| int choice, slot, sc; |
| char hexbuf[20]; |
|
|
| while (scanf("%d", &choice) == 1) { |
| switch (choice) { |
| case 1: |
| if (scanf("%d %d", &slot, &sc) != 2) break; |
| if (slot < 0 || slot >= MAX_SLOTS) { puts("ERR"); break; } |
| if (sc < 1 || sc > SIZES_COUNT) { puts("ERR"); break; } |
| if (slots[slot].data != NULL) { puts("ERR"); break; } |
| slots[slot].data = malloc(sizes[sc-1]); |
| if (!slots[slot].data) { puts("ERR"); break; } |
| memset(slots[slot].data, 0, sizes[sc-1]); |
| slots[slot].size_class = sc; |
| slots[slot].active = 1; |
| puts("OK"); |
| break; |
|
|
| case 2: |
| if (scanf("%d %16s", &slot, hexbuf) != 2) break; |
| if (slot < 0 || slot >= MAX_SLOTS || !slots[slot].data) { puts("ERR"); break; } |
| |
| { |
| unsigned char bytes[8] = {0}; |
| int len = strlen(hexbuf) / 2; |
| if (len > 8) len = 8; |
| for (int i = 0; i < len; i++) { |
| unsigned int b; |
| sscanf(hexbuf + i*2, "%2x", &b); |
| bytes[i] = (unsigned char)b; |
| } |
| memcpy(slots[slot].data, bytes, len); |
| } |
| puts("OK"); |
| break; |
|
|
| case 3: |
| if (scanf("%d", &slot) != 1) break; |
| if (slot < 0 || slot >= MAX_SLOTS || !slots[slot].data) { puts("ERR"); break; } |
| if (!slots[slot].active) { |
| puts("DELETED"); |
| break; |
| } |
| { |
| unsigned char *p = (unsigned char *)slots[slot].data; |
| for (int i = 0; i < 8; i++) printf("%02x", p[i]); |
| puts(""); |
| } |
| break; |
|
|
| case 4: |
| if (scanf("%d", &slot) != 1) break; |
| if (slot < 0 || slot >= MAX_SLOTS || !slots[slot].data) { puts("ERR"); break; } |
| free(slots[slot].data); |
| |
| slots[slot].active = 0; |
| puts("OK"); |
| break; |
|
|
| case 5: |
| goto done; |
|
|
| default: |
| puts("ERR"); |
| break; |
| } |
| } |
| done: |
| return 0; |
| } |
|
|