Unify process_alloc() and process_realloc() with PROCESS_ALLOC_MODE_REALLOC bit
[moveable_pool.git] / util.c
1 #include <assert.h> // Nick
2 #include <stdbool.h> // Nick
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11
12 #include "util.h"
13
14 int dev_fd;
15 int dev_offset;
16
17 int swizzling;
18
19 int fd_open(char *name)
20 {
21         char *namecopy, *sd;
22         int bias = 0;
23
24         namecopy = strdup(name);
25         sd = index(namecopy, ':');
26         if (sd) {
27                 *sd = 0;
28                 sd++;
29                 bias = atoi(sd);
30         }
31
32         printf("Opening %s (offset %d)\n", namecopy, bias);
33         dev_offset = bias;
34         dev_fd = open(namecopy, O_RDWR | O_CREAT, 0666);
35         free(namecopy);
36
37         if (dev_fd < 0)
38                 return -1;
39         /* printf("fd=%d, dev_offset = %d\n", dev_fd, dev_offset); */
40         return 0;
41 }
42
43
44 void panic(char *s)
45 {
46         fprintf(stderr, "panic: %s\n", s);
47  assert(false);
48         exit(1);
49 }
50
51 uint16_t swizzle16(uint32_t v)
52 {
53         int top = v & 0xFFFF0000UL;
54         if (top && top != 0xFFFF0000) {
55                 fprintf(stderr, "swizzle16 given a 32bit input\n");
56                 exit(1);
57         }
58         if (swizzling)
59                 return (v & 0xFF) << 8 | ((v & 0xFF00) >> 8);
60         else
61                 return v;
62 }
63
64 uint32_t swizzle32(uint32_t v)
65 {
66         if (!swizzling)
67                 return v;
68
69         return (v & 0xFF) << 24 | (v & 0xFF00) << 8 | (v & 0xFF0000) >> 8 |
70             (v & 0xFF000000) >> 24;
71 }