xref: /haiku/src/kits/media/ChunkCache.cpp (revision fef6144999c2fa611f59ee6ffe6dd7999501385c)
1 /*
2  * Copyright (c) 2004, Marcus Overhagen
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  *  * Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23  * OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include <Locker.h>
26 
27 #include "ChunkCache.h"
28 #include "debug.h"
29 
30 
31 ChunkCache::ChunkCache()
32 {
33 	fLocker = new BLocker("media chunk cache locker");
34 
35 	// fEmptyChunkCount must be one less than the real chunk count,
36 	// because the buffer returned by GetNextChunk must be preserved
37 	// until the next call of that function, and must not be overwritten.
38 	fEmptyChunkCount = CHUNK_COUNT - 1;
39 	fReadyChunkCount = 0;
40 	fNeedsRefill = 1;
41 
42 	fGetWaitSem = create_sem(0, "media chunk cache sem");
43 
44 	fNextPut = &fChunkInfos[0];
45 	fNextGet = &fChunkInfos[0];
46 
47 	for (int i = 0; i < CHUNK_COUNT; i++) {
48 		fChunkInfos[i].next = (i == CHUNK_COUNT -1) ? &fChunkInfos[0] : &fChunkInfos[i + 1];
49 		fChunkInfos[i].buffer = NULL;
50 		fChunkInfos[i].sizeUsed = 0;
51 		fChunkInfos[i].sizeMax = 0;
52 		fChunkInfos[i].err = B_ERROR;
53 	}
54 }
55 
56 
57 ChunkCache::~ChunkCache()
58 {
59 	delete_sem(fGetWaitSem);
60 	delete fLocker;
61 	for (int i = 0; i < CHUNK_COUNT; i++) {
62 		free(fChunkInfos[i].buffer);
63 	}
64 }
65 
66 
67 void
68 ChunkCache::MakeEmpty()
69 {
70 	fLocker->Lock();
71 	fEmptyChunkCount = CHUNK_COUNT - 1;
72 	fReadyChunkCount = 0;
73 	fNextPut = &fChunkInfos[0];
74 	fNextGet = &fChunkInfos[0];
75 	atomic_or(&fNeedsRefill, 1);
76 	fLocker->Unlock();
77 }
78 
79 
80 bool
81 ChunkCache::NeedsRefill()
82 {
83 	return atomic_or(&fNeedsRefill, 0);
84 }
85 
86 
87 status_t
88 ChunkCache::GetNextChunk(const void **chunkBuffer, size_t *chunkSize, media_header *mediaHeader)
89 {
90 //	printf("ChunkCache::GetNextChunk: %p fEmptyChunkCount %ld, fReadyChunkCount %ld\n", fNextGet, fEmptyChunkCount, fReadyChunkCount);
91 retry:
92 	acquire_sem(fGetWaitSem);
93 
94 	fLocker->Lock();
95 	if (fReadyChunkCount == 0) {
96 		fLocker->Unlock();
97 		printf("ChunkCache::GetNextChunk: %p retrying\n", fNextGet);
98 		goto retry;
99 	}
100 	fEmptyChunkCount++;
101 	fReadyChunkCount--;
102 	atomic_or(&fNeedsRefill, 1);
103 	fLocker->Unlock();
104 
105 	*chunkBuffer = fNextGet->buffer;
106 	*chunkSize = fNextGet->sizeUsed;
107 	*mediaHeader = fNextGet->mediaHeader;
108 	status_t err = fNextGet->err;
109 	fNextGet = fNextGet->next;
110 
111 	return err;
112 }
113 
114 
115 void
116 ChunkCache::PutNextChunk(const void *chunkBuffer, size_t chunkSize, const media_header &mediaHeader, status_t err)
117 {
118 //	printf("ChunkCache::PutNextChunk: %p fEmptyChunkCount %ld, fReadyChunkCount %ld\n", fNextPut, fEmptyChunkCount, fReadyChunkCount);
119 
120 	if (err == B_OK) {
121 		if (fNextPut->sizeMax < chunkSize) {
122 //			printf("ChunkCache::PutNextChunk: %p resizing from %ld to %ld\n", fNextPut, fNextPut->sizeMax, chunkSize);
123 			free(fNextPut->buffer);
124 			fNextPut->buffer = malloc((chunkSize + 1024) & ~1023);
125 			fNextPut->sizeMax = chunkSize;
126 		}
127 		memcpy(fNextPut->buffer, chunkBuffer, chunkSize);
128 		fNextPut->sizeUsed = chunkSize;
129 	}
130 
131 	fNextPut->mediaHeader = mediaHeader;
132 	fNextPut->err = err;
133 
134 	fNextPut = fNextPut->next;
135 
136 	fLocker->Lock();
137 	fEmptyChunkCount--;
138 	fReadyChunkCount++;
139 	if (fEmptyChunkCount == 0)
140 		atomic_and(&fNeedsRefill, 0);
141 	fLocker->Unlock();
142 
143 	release_sem(fGetWaitSem);
144 }
145