Commit d99bb6b7 authored by Kirill Smelkov's avatar Kirill Smelkov

libgolang: Switch _makechan to panic instead of return NULL on failure

- it is in Go style to panic on memory allocation failure.
- _makechan can already panic, instead of returning NULL, on e.g. ch._mu
  initialization failure.
- we were already not checking in several places for NULL after
  _makechan() call (e.g. in golang/runtime/libgolang_test_c.c).

This switches _makechan to panic on memory allocation failure and
settles on style of future C-level API calls to panic instead of
returning NULL on failure.
parent c489bd5f
...@@ -359,8 +359,6 @@ chan<T> makechan(unsigned size) { ...@@ -359,8 +359,6 @@ chan<T> makechan(unsigned size) {
? 0 // eg struct{} for which sizeof() gives 1 - *not* 0 ? 0 // eg struct{} for which sizeof() gives 1 - *not* 0
: sizeof(T); : sizeof(T);
ch._ch = _makechan(elemsize, size); ch._ch = _makechan(elemsize, size);
if (ch._ch == NULL)
throw std::bad_alloc();
return ch; return ch;
} }
......
...@@ -417,11 +417,12 @@ _RecvSendWaiting *_dequeWaiter(list_head *queue) { ...@@ -417,11 +417,12 @@ _RecvSendWaiting *_dequeWaiter(list_head *queue) {
// _makechan creates new _chan(elemsize, size). // _makechan creates new _chan(elemsize, size).
// //
// returned channel has refcnt=1. // returned channel has refcnt=1.
// _makechan always returns !NULL and panics on memory allocation failure.
_chan *_makechan(unsigned elemsize, unsigned size) { _chan *_makechan(unsigned elemsize, unsigned size) {
_chan *ch; _chan *ch;
ch = (_chan *)zalloc(sizeof(_chan) + size*elemsize); ch = (_chan *)zalloc(sizeof(_chan) + size*elemsize);
if (ch == NULL) if (ch == NULL)
return NULL; panic("makechan: alloc failed");
new (&ch->_mu) Mutex(); new (&ch->_mu) Mutex();
ch->_refcnt = 1; ch->_refcnt = 1;
......
...@@ -74,8 +74,6 @@ struct _work_arg{int i; _chan *done;}; ...@@ -74,8 +74,6 @@ struct _work_arg{int i; _chan *done;};
static void _work(void *); static void _work(void *);
void _test_go_c(void) { void _test_go_c(void) {
_chan *done = _makechan(0,0); _chan *done = _makechan(0,0);
if (done == NULL)
panic("_makechan -> failed");
struct _work_arg *_ = malloc(sizeof(*_)); struct _work_arg *_ = malloc(sizeof(*_));
if (_ == NULL) if (_ == NULL)
panic("malloc _work_arg -> failed"); panic("malloc _work_arg -> failed");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment