/
HFByteSliceFileOperationQueueEntry.m
53 lines (43 loc) · 1.46 KB
/
HFByteSliceFileOperationQueueEntry.m
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
//
// HFByteSliceFileOperationQueueEntry.m
// HexFiend_2
//
// Copyright 2008 ridiculous_fish. All rights reserved.
//
#import "HFByteSliceFileOperationQueueEntry.h"
#include <malloc/malloc.h>
#import <HexFiend/HFFrameworkPrefix.h>
#import <HexFiend/HFFunctions.h>
#import <HexFiend/HFFunctions_Private.h>
#define SOFT_MAX_PER_BUFFER (512u * 1024u)
@implementation HFByteSliceFileOperationQueueEntry
@end
@implementation HFByteSliceFileOperationContext
- (void *)allocateMemoryOfLength:(NSUInteger)len {
HFASSERT(len > 0);
void *result = check_malloc(len);
totalAllocatedMemory = HFSumInt(totalAllocatedMemory, len);
return result;
}
- (void)freeMemory:(void *)buff ofLength:(NSUInteger)len {
HFASSERT(buff == NULL || len > 0);
if (buff == NULL && len == 0) return;
HFASSERT(len <= malloc_size(buff));
HFASSERT(len <= totalAllocatedMemory);
totalAllocatedMemory -= len;
free(buff);
}
- (NSUInteger)suggestedAllocationLengthForMinimum:(NSUInteger)minimum maximum:(NSUInteger)maximum {
HFASSERT(maximum >= minimum);
NSUInteger minAllocatable = softMaxAllocatedMemory - MIN(softMaxAllocatedMemory, totalAllocatedMemory);
NSUInteger result = maximum, paddedResult;
result = MIN(minAllocatable, result);
result = MIN(SOFT_MAX_PER_BUFFER, result);
result = MAX(result, minimum);
HFASSERT(result >= minimum && result <= maximum);
paddedResult = malloc_good_size(result);
HFASSERT(paddedResult >= result);
result = MIN(paddedResult, maximum);
return result;
}
@end