-
Notifications
You must be signed in to change notification settings - Fork 25
/
aligned.c
123 lines (110 loc) · 4.21 KB
/
aligned.c
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
/*
* aligned.c
* $Id: aligned.c,v 1.8 2006/09/01 17:18:31 bobi Exp $
*
* Copyright 2004 Bobi B., [email protected]
*
* This file is part of hdl_dump.
*
* hdl_dump is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* hdl_dump is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with hdl_dump; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "aligned.h"
struct aligned_type
{
osal_handle_t in;
char /*@owned@*/ *unaligned;
char /*@dependent@*/ *buffer;
u_int32_t sector_size, buffer_size, data_length;
u_int64_t offset;
};
/**************************************************************/
aligned_t *
al_alloc(osal_handle_t in,
u_int32_t sector_size,
u_int32_t buffer_size_in_sectors)
{
aligned_t *al = osal_alloc(sizeof(aligned_t));
if (al != NULL) {
memset(al, 0, sizeof(aligned_t));
al->buffer_size = sector_size * buffer_size_in_sectors;
al->unaligned = osal_alloc(al->buffer_size + sector_size);
if (al->unaligned != NULL) {
al->in = in;
/* FIX: 64-bit OS compatibility (not to trim ptr to 32-bit) */
al->buffer = (void *)(((unsigned long)al->unaligned + sector_size - 1) & ~((unsigned long)sector_size - 1));
assert(al->buffer >= al->unaligned);
al->sector_size = sector_size;
al->offset = (u_int64_t)-1;
} else {
osal_free(al);
al = NULL;
}
}
return (al);
}
/**************************************************************/
void al_free(aligned_t *al)
{
if (al != NULL) {
osal_free(al->unaligned);
osal_free(al);
}
}
/**************************************************************/
int al_read(aligned_t *al,
u_int64_t offset,
const char **data,
u_int32_t bytes,
u_int32_t *data_length)
{
int result = OSAL_OK;
/* check if buffer contains all requested data */
if (al->offset <= offset && offset + bytes <= al->offset + al->data_length) { /* return data from the buffer */
*data = al->buffer + (offset - al->offset);
*data_length = bytes;
} else { /* buffer does not contains all or any of the requested data */
u_int64_t aligned_offset = offset & ~((u_int64_t)al->sector_size - 1);
u_int32_t correction = 0;
assert(aligned_offset <= offset);
/* check whether cache contains some usable data */
if (al->offset <= aligned_offset &&
aligned_offset < al->offset + al->data_length) { /* arrange data inside the cache and correct the offset */
u_int32_t usable_data_offs = (u_int32_t)(aligned_offset - al->offset);
u_int32_t usable_data_len = (u_int32_t)(al->offset + al->data_length - aligned_offset);
memmove(al->buffer, al->buffer + usable_data_offs, usable_data_len);
correction = usable_data_len;
}
result = osal_seek(al->in, aligned_offset + correction);
if (result == OSAL_OK) {
result = osal_read(al->in, al->buffer + correction,
al->buffer_size - correction, &al->data_length);
al->data_length += correction;
#if 0
printf ("osal_read (%d, %luKB, %d, -> %d) = %d\n",
al->in, (long) (aligned_offset / 1024), al->buffer_size, al->data_length, result);
#endif
}
if (result == OSAL_OK) { /* success */
u_int32_t skip = (u_int32_t)(offset - aligned_offset);
al->offset = aligned_offset;
*data = al->buffer + skip;
*data_length = bytes > al->data_length - skip ? al->data_length - skip : bytes;
}
}
return (result);
}