summaryrefslogtreecommitdiff
path: root/src/misc.c
blob: 686e46013d09831333e0085b78528dcede3c7938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* 
 * AUTHOR
 * N. Nielsen
 *
 * LICENSE
 * This software is in the public domain.
 *
 * The software is provided "as is", without warranty of any kind,
 * express or implied, including but not limited to the warranties
 * of merchantability, fitness for a particular purpose, and
 * noninfringement. In no event shall the author(s) be liable for any
 * claim, damages, or other liability, whether in an action of
 * contract, tort, or otherwise, arising from, out of, or in connection
 * with the software or the use or other dealings in the software.
 * 
 * SUPPORT
 * Send bug reports to: <nielsen@memberwebs.com>
 */

#include "usuals.h"
#include "compat.h"
#include "memref.h"
#include "locks.h"


/* These locks are used to signify which */
struct drivelock
{
	uint64 beg;
	uint64 end;
};

void addLocationLock(drivelocks* locks, uint64 beg, uint64 end)
{
  uint32 i;

	if(locks->_count <= locks->_current)
	{
		locks->_count += 0x400;
		locks->_locks = (struct drivelock*)reallocf(locks->_locks, sizeof(struct drivelock) * locks->_count);
  }

	/* TODO: Implement a more efficient method here! */
	if(locks->_locks)
	{
		/* Go through and check for a current lock we can tag onto */
		for(i = 0; i < locks->_current; i++)
		{
			if(INTERSECTS(locks->_locks[i].beg, locks->_locks[i].end, beg, end))
      {
        locks->_locks[i].beg = min(locks->_locks[i].beg, beg);
        locks->_locks[i].end = max(locks->_locks[i].end, end);
        return;
      }
		}

		locks->_locks[locks->_current].beg = beg;
		locks->_locks[locks->_current].end = end;
		locks->_current++;
	}
}

bool checkLocationLock(drivelocks* locks, uint64 sec)
{
  uint32 i;

  if(locks->_locks)
  {
  	/* Go through and check for a lock */
	  for(i = 0; i < locks->_current; i++)
	  {
		  if(sec >= locks->_locks[i].beg && 
			  sec < locks->_locks[i].end)
		  {
			  sec = locks->_locks[i].end;
			  return true;
		  }
    }
	}

	return false;
}

#ifdef _DEBUG
void dumpLocationLocks(drivelocks* locks)
{
  uint32 i;

	for(i = 0; i < locks->_current; i++)
		printf("%u\t%u\n", (uint32)locks->_locks[i].beg, (uint32)locks->_locks[i].end);

	printf("\n");
}
#endif




/*
 * WARNING!! Not thread safe or very efficient for large
 * amounts of memory allocations
 */

#ifdef _DEBUG
const size_t kRefSig = 0x1F2F3F4F;

void* _refalloc_dbg(size_t sz)
{
	/* Allocate extra counter value before memory */
	size_t* mem = (size_t*)mallocf(sz * sizeof(size_t) * 2);

	if(mem)
	{
		mem[0] = kRefSig;
		mem[1] = 1;
		return mem + 2;
	}

	return mem;
}
#endif 

void* _refalloc(size_t sz)
{
	/* Allocate extra counter value before memory */
	size_t* mem = (size_t*)mallocf(sz * sizeof(size_t) * 1);

	if(mem)
	{
		mem[0] = 1;
		return mem + 1;
	}

	return mem;
}

#ifdef _DEBUG
void* _refadd_dbg(void* buf)
{
	if(buf)
	{
		/* Increment the counter value */
		size_t* mem = (size_t*)buf - 2;
		assert(mem[0] == kRefSig);
		mem[1]++;
	}

	return buf;
}
#endif

void* _refadd(void* buf)
{
	if(buf)
		/* Increment the counter value */
		((size_t*)buf)[-1]++;

	return buf;
}

#ifdef _DEBUG
void _refrelease_dbg(void* buf)
{
	if(buf)
	{
		/* Decrement the counter value */
		size_t* mem = (size_t*)buf - 2;
		assert(mem[0] == kRefSig);

		if(!--mem[1])
			free(mem);
	}
}
#endif

void _refrelease(void* buf)
{
	if(buf)
	{
		/* Decrement the counter value */
		size_t* mem = (size_t*)buf - 1;

		if(!--mem[0])
			free(mem);
	}
}

#define COMPARE_BLOCK_SIZE  4096

int compareFileData(int f, void* data, size_t length)
{
  unsigned char buf[COMPARE_BLOCK_SIZE];
  unsigned char* d = (unsigned char*)data;
  int num, r;

  while(length > 0)
  {
    num = min(COMPARE_BLOCK_SIZE, length);
    r = read(f, buf, num);

    if(r < 0)
      err(1, "error reading comparison file");

    if(r < num)
      return -1;

    r = memcmp(d, buf, num);
    if(r != 0)
      return r;

    d += num;
    length -= num;
  }

  return 0;
}