Error handling in _create() functions

This commit is contained in:
Jean-Marc Valin 2011-01-30 10:23:40 -05:00
parent 913a1742b9
commit d6c3d3ceae

View file

@ -156,18 +156,26 @@ int celt_encoder_get_size(const CELTMode *mode, int channels)
CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error) CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error)
{ {
CELTEncoder *st;
CELTMode *mode = celt_mode_create(48000, 960, NULL); CELTMode *mode = celt_mode_create(48000, 960, NULL);
CELTEncoder *st = celt_encoder_init( st = (CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels));
(CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels)), if (st!=NULL && celt_encoder_init(st, sampling_rate, channels, error)==NULL)
sampling_rate, channels, error); {
celt_encoder_destroy(st);
st = NULL;
}
return st; return st;
} }
CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error) CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error)
{ {
return celt_encoder_init_custom( CELTEncoder *st = (CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels));
(CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels)), if (st!=NULL && celt_encoder_init_custom(st, mode, channels, error)==NULL)
mode, channels, error); {
celt_encoder_destroy(st);
st = NULL;
}
return st;
} }
CELTEncoder *celt_encoder_init(CELTEncoder *st, int sampling_rate, int channels, int *error) CELTEncoder *celt_encoder_init(CELTEncoder *st, int sampling_rate, int channels, int *error)
@ -192,7 +200,7 @@ CELTEncoder *celt_encoder_init_custom(CELTEncoder *st, const CELTMode *mode, int
return NULL; return NULL;
} }
if (st==NULL) if (st==NULL || mode==NULL)
{ {
if (error) if (error)
*error = CELT_ALLOC_FAIL; *error = CELT_ALLOC_FAIL;
@ -1754,17 +1762,26 @@ int celt_decoder_get_size(const CELTMode *mode, int channels)
CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error) CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error)
{ {
CELTDecoder *st;
const CELTMode *mode = celt_mode_create(48000, 960, NULL); const CELTMode *mode = celt_mode_create(48000, 960, NULL);
return celt_decoder_init( st = (CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels));
(CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels)), if (st!=NULL && celt_decoder_init(st, sampling_rate, channels, error)==NULL)
sampling_rate, channels, error); {
celt_decoder_destroy(st);
st = NULL;
}
return st;
} }
CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error) CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error)
{ {
return celt_decoder_init_custom( CELTDecoder *st = (CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels));
(CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels)), if (st!=NULL && celt_decoder_init_custom(st, mode, channels, error)==NULL)
mode, channels, error); {
celt_decoder_destroy(st);
st = NULL;
}
return st;
} }
CELTDecoder *celt_decoder_init(CELTDecoder *st, int sampling_rate, int channels, int *error) CELTDecoder *celt_decoder_init(CELTDecoder *st, int sampling_rate, int channels, int *error)