summaryrefslogtreecommitdiff
path: root/inc/slices.h
blob: b9946362b1847535480ff04193d35bf55a7c3adc (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
#ifndef SLICES_H
#define SLICES_H

#include <stdint.h>
#include <stdlib.h>
#include <pthread.h>

/* IMPORTANT NOTES
Slice are inclusive intervals. Let say sliceNew(1,2,S_UNKNOWN,NULL) return a [1;2] interval,
 so interval lenght is end-begin+1. Here, it is 2 sectors lenght slice.
*/

typedef enum { S_UNKNOWN, S_RECOVERED, S_UNREADABLE } sliceStatus_t;
typedef unsigned long long int address_t;

typedef struct _slice {
	address_t begin, end;
	sliceStatus_t status;
	struct _slice *next;
} slice_t;

typedef struct {
	int count;
	slice_t *first, *last;
	address_t min, max;
	pthread_mutex_t writeOrConsistentReadMutex;
} slices_t;

slice_t *sliceNew(address_t begin, address_t end, sliceStatus_t status, slice_t *next);
void sliceDelete(slice_t *s);

// Return the numbers of slices after split (3 in the general case, 2 or 1 in particular cases. -1 is memory error)
int sliceSplit(slices_t *slices, slice_t *initialSlice, address_t splitAt, sliceStatus_t statusBefore, sliceStatus_t statusAt, sliceStatus_t statusAfter);

void sliceDumpUpdate(char *dump, slice_t *s, address_t blockSize, unsigned int charCount, address_t begin, address_t end);

slices_t *slicesNewEmpty();

slices_t *slicesNewSingleton(address_t begin, address_t end, sliceStatus_t status);
void slicesDelete(slices_t *slices);

void slicesAppend(slices_t *slices, slice_t *slice);

slice_t *slicesFindLargest(slices_t *slices, sliceStatus_t status);

slice_t *slicesFindLargestFast(slices_t *slices, address_t *foundMax, sliceStatus_t status, address_t knownMax, slice_t *firstToTry);

char *slicesDump(slices_t *slices, address_t *blockSize, unsigned int charCount, address_t begin, address_t end);

#endif /*SLICES_H*/