Skip to content

Commit 6e8fb7b

Browse files
committed
Resolve memory exceptions
1 parent dc45243 commit 6e8fb7b

File tree

2 files changed

+27
-40
lines changed

2 files changed

+27
-40
lines changed

src/CommonHelix.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#ifndef HELIX_LOGGING_ACTIVE
66
#define HELIX_LOGGING_ACTIVE true
77
#endif
8+
#ifndef HELIX_LOG_LEVEL
9+
#define HELIX_LOG_LEVEL Info
10+
#endif
811

912
#define SYNCH_WORD_LEN 4
1013

@@ -80,7 +83,7 @@ class CommonHelix {
8083
}
8184
memset(frame_buffer,0, maxFrameSize()+1);
8285
memset(pwm_buffer,0, maxPWMSize());
83-
pwm_buffer[maxPWMSize()+1]=-1;
86+
pwm_buffer[maxPWMSize()]=-1;
8487
active = true;
8588
}
8689

@@ -156,15 +159,10 @@ class CommonHelix {
156159

157160
/// we add the data to the buffer until it is full
158161
size_t appendToBuffer(const void *in_ptr, int in_size){
159-
LOG(Debug, "appendToBuffer: %d", in_size);
160-
checkMemory();
162+
LOG(Info, "appendToBuffer: %d (at %p)", in_size, frame_buffer);
161163
int buffer_size_old = buffer_size;
162164
int process_size = min((int)(maxFrameSize() - buffer_size), in_size);
163-
assert(in_size>=0);
164-
assert(in_size>=0);
165-
assert(buffer_size>=0);
166-
memmove(frame_buffer+buffer_size_old, in_ptr, process_size);
167-
checkMemory();
165+
memmove(frame_buffer+buffer_size, in_ptr, process_size);
168166
buffer_size += process_size;
169167
LOG(Debug, "appendToBuffer %d + %d -> %u", buffer_size_old, process_size, buffer_size );
170168
return process_size;
@@ -205,7 +203,6 @@ class CommonHelix {
205203
memmove(frame_buffer, frame_buffer + range.start, buffer_size);
206204
range.end -= range.start;
207205
range.start = 0;
208-
checkMemory();
209206
LOG(Debug, "-> we are at beginning of synch word");
210207
} else if (range.start==0) {
211208
LOG(Debug, "-> we are at beginning of synch word");
@@ -226,13 +223,9 @@ class CommonHelix {
226223
return result;
227224
}
228225

229-
virtual void checkMemory(){
230-
}
231-
232226
void advanceFrameBuffer(int offset){
233227
buffer_size -= offset;
234228
memmove(frame_buffer, frame_buffer+offset, buffer_size);
235-
checkMemory();
236229
}
237230

238231
};

src/MP3DecoderHelix.h

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ class MP3DecoderHelix : public CommonHelix {
108108
void decode(Range r) {
109109
LOG(Debug, "decode %d", r.end);
110110
int len = r.end;
111-
int bytesLeft = r.end; //r.end; //r.end; // buffer_size
112-
113-
int result = MP3Decode(decoder, &frame_buffer + r.start, &bytesLeft, pwm_buffer, mp3_type);
114-
checkMemory();
111+
int bytesLeft = r.end-r.start; //r.end; //r.end; // buffer_size
115112

113+
uint8_t* ptr = frame_buffer + r.start;
114+
int result = MP3Decode(decoder, &ptr, &bytesLeft, pwm_buffer, mp3_type);
116115

116+
int decoded = len - bytesLeft;
117+
117118
if (result==0){
118119
int decoded = len - bytesLeft;
119120
LOG(Debug, "-> bytesLeft %d -> %d = %d ", buffer_size, bytesLeft, decoded);
@@ -127,44 +128,37 @@ class MP3DecoderHelix : public CommonHelix {
127128
// remove processed data from buffer
128129
buffer_size -= decoded;
129130
memmove(frame_buffer, frame_buffer+r.start+decoded, buffer_size);
130-
checkMemory();
131131
LOG(Debug, " -> decoded %d bytes - remaining buffer_size: %d", decoded, buffer_size);
132132
} else {
133133
// decoding error
134134
LOG(Debug, " -> decode error: %d - removing frame!", result);
135+
int ignore = decoded;
136+
if (ignore == 0) ignore = r.end;
135137
// We advance to the next synch world
136-
if (r.end>0){
137-
buffer_size -= r.end;
138-
memmove(frame_buffer, frame_buffer+r.end, buffer_size);
139-
checkMemory();
140-
}
138+
buffer_size -= ignore;
139+
memmove(frame_buffer, frame_buffer+ignore, buffer_size);
141140
}
142141
}
143142

144143
// return the resulting PWM data
145144
void provideResult(MP3FrameInfo &info){
146145
LOG(Debug, "=> provideResult: %d", info.outputSamps);
147-
148-
// provide result
149-
if(pwmCallback!=nullptr){
150-
// output via callback
151-
pwmCallback(info, pwm_buffer, info.outputSamps);
152-
} else {
153-
// output to stream
154-
if (info.samprate!=mp3FrameInfo.samprate && infoCallback!=nullptr){
155-
infoCallback(mp3FrameInfo);
146+
if (info.outputSamps>0){
147+
// provide result
148+
if(pwmCallback!=nullptr){
149+
// output via callback
150+
pwmCallback(info, pwm_buffer, info.outputSamps);
151+
} else {
152+
// output to stream
153+
if (info.samprate!=mp3FrameInfo.samprate && infoCallback!=nullptr){
154+
infoCallback(mp3FrameInfo);
155+
}
156+
out->write((uint8_t*)pwm_buffer, info.outputSamps);
156157
}
157-
out->write((uint8_t*)pwm_buffer, info.outputSamps);
158+
mp3FrameInfo = info;
158159
}
159-
mp3FrameInfo = info;
160-
checkMemory();
161160
}
162161

163-
/// checks the consistency of the memory
164-
virtual void checkMemory(){
165-
//assert(frame_buffer[maxFrameSize()+1]==0);
166-
//assert(pwm_buffer[maxPWMSize()+1]==-1);
167-
}
168162

169163
};
170164

0 commit comments

Comments
 (0)