예제 #1
0
파일: memory_pool.c 프로젝트: onmsr/slim
void *memory_pool_malloc(memory_pool *p)
{
  void *res;

  if (p->free_head == 0) {
    char *rawblock;
    int i;

    /* fprintf(stderr, "no more free space, so allocate new block\n"); */

    /* top of block is used as pointer to head of next block */
    rawblock = lmn_malloc(ALIGNED_SIZE(sizeof(void*)) + p->sizeof_element * blocksize);
    *(void**)rawblock = p->block_head;
    p->block_head = rawblock;

    /* rest is used as space for elements */
    /* skip top of block */
    rawblock = rawblock + ALIGNED_SIZE(sizeof(void*));
    p->free_head = rawblock;

    for (i = 0; i < (blocksize - 1); i++) {
      /* top of each empty elements is used as pointer to next empty element */
      REF_CAST(void*, rawblock[p->sizeof_element * i]) = &rawblock[p->sizeof_element * (i + 1)];
    }
    REF_CAST(void*, rawblock[p->sizeof_element * (blocksize - 1)]) = 0;
  }

  res = p->free_head;
  p->free_head = *(void**)p->free_head;

  return res;
}
예제 #2
0
static int Add_pack_msg_no_seek (Link_struct *link, Req_struct *req,
						char *msgp, int msize) {
    int ret, size;
    CM_resp_struct resp;
    extern int CMC_comm_resp_event;	/* defined in cmc_common.c */ 

    if (link->pack_cnt == 0) {		/* set start time */
	int ms;
	time_t cr_t = MISC_systime (&ms);
	link->pack_st_time = (double)cr_t + .001 * ms;
/*	if (link->saved_msgs == NULL) */
	    link->saved_msgs = STR_reset (link->saved_msgs, 40000);
	link->n_saved_bytes = 0;
    }

    /* read the full message and save it */
    size = sizeof (CM_req_struct) + req->data_size;
    link->saved_msgs = STR_append (link->saved_msgs, NULL, 
				ALIGNED_SIZE (size) + sizeof (int));
    if (msgp == NULL) {
	LB_seek (link->reqfd, -1, LB_CURRENT, NULL);
	ret = LB_read (link->reqfd,
		link->saved_msgs + link->n_saved_bytes + sizeof (int),
							size, LB_NEXT);
    }
    else
	ret = msize;

    if (ret != size) {
	LE_send_msg (LE_VL0, "Bad request (type %d) - ignored", req->type);
	return (0);
    }
    if (msgp != NULL)
	memcpy (link->saved_msgs + link->n_saved_bytes + sizeof (int),
								msgp, size);

    *((int *)(link->saved_msgs + link->n_saved_bytes)) = size;
    link->n_saved_bytes += ALIGNED_SIZE (size) + sizeof (int);
    link->pack_cnt++;

    resp.type = req->type;
    resp.req_num = req->req_num;
    resp.link_ind = req->link_ind;
    resp.ret_code = CM_SUCCESS;
    resp.data_size = 0;
    resp.time = time (NULL);

    ret = LB_write (link->respfd, (char *)&resp, 
				sizeof (CM_resp_struct), LB_NEXT);
    if (ret != sizeof (CM_resp_struct)) {
	LE_send_msg (GL_ERROR,  "LB_write (response 1) failed (ret = %d)", ret);
	CM_terminate ();
    }
    if (CMC_get_en_flag () >= 0)
	EN_post (CMC_comm_resp_event + link->link_ind, 
					NULL, 0, CMC_get_en_flag ());
    return (1);
}
예제 #3
0
/**
 * \sa HDK_CFG_MEMORY_FOR_MALLOC
 */
void *haddock_malloc(os_size_t s)
{
    os_size_t size = ALIGNED_SIZE(s);
    haddock_assert(size < HDK_CFG_MEMORY_FOR_MALLOC
                   && size < MAX_MEMORY_BLK_SIZE);
    
    os_uint32 idx = 0;
    struct __memory_blk *blk = NULL;
    os_boolean found = OS_FALSE;
    
    while (!found) {
        if (idx > HDK_CFG_MEMORY_FOR_MALLOC - (sizeof(struct __memory_blk) + size))
            break;
        blk = (struct __memory_blk *) (& __haddock_memory_allocation[idx]);
        if ((blk->hdr & BV(31)) == BV(31)) {
            // has been used
            idx += sizeof(struct __memory_blk) + (blk->hdr & 0x00FFFFFF);
            continue;
        } else {
            // has not been used
            blk->hdr |= BV(31);
            blk->hdr &= 0xFF000000;
            blk->hdr |= size;
            found = OS_TRUE;
            _update_hdk_malloc_usage(sizeof(struct __memory_blk) + size);
            break;
        }
    }
    
    if (found)
        return blk->blk;
    else
        return NULL;
}
예제 #4
0
파일: gc.c 프로젝트: stesla/ferrite
static void *gc_alloc(size_t bytes) {
  void *result;
  if ((result = malloc(ALIGNED_SIZE(bytes))) == NULL) {
    fprintf(stderr, "out of memory");
    abort();
  }
  return result;
}
예제 #5
0
파일: alloc.c 프로젝트: Distrotech/cmucl
lispobj
alloc_cons(lispobj car, lispobj cdr)
{
    struct cons *ptr = (struct cons *) alloc(ALIGNED_SIZE(sizeof(struct cons)));

    ptr->car = car;
    ptr->cdr = cdr;

    return (lispobj) ptr | type_ListPointer;
}
예제 #6
0
파일: alloc.c 프로젝트: Distrotech/cmucl
static lispobj *
alloc_unboxed(int type, int words)
{
    lispobj *result;

    result = (lispobj *) alloc(ALIGNED_SIZE((1 + words) * sizeof(lispobj)));

    *result = (lispobj) (words << type_Bits) | type;

    return result;
}
예제 #7
0
파일: memory_pool.c 프로젝트: onmsr/slim
memory_pool *memory_pool_new(int s)
{
  memory_pool *res = LMN_MALLOC(memory_pool);

  res->sizeof_element = ALIGNED_SIZE(s);
  res->block_head = 0;
  res->free_head = 0;

  /* fprintf(stderr, "this memory_pool allocate %d, aligned as %d\n", s, res->sizeof_element); */

  return res;
}
예제 #8
0
int CMC_align_bytes (int n_bytes)
{
    int tmp;

    tmp = n_bytes;
    if (tmp < 0)
	tmp = -tmp;
    tmp = ALIGNED_SIZE (tmp);
    if (n_bytes >= 0)
	return (tmp);
    else
	return (-tmp);
}
예제 #9
0
파일: alloc.c 프로젝트: Distrotech/cmucl
static lispobj
alloc_vector(int type, int length, int size)
{
    struct vector *result;

    result = (struct vector *)
	alloc(ALIGNED_SIZE((2 + (length * size + 31) / 32) * sizeof(lispobj)));

    result->header = type;
    result->length = make_fixnum(length);

    return ((lispobj) result) | type_OtherPointer;
}
예제 #10
0
/*
 * Entry point for getaddrinfo
 * we convert the UTF-8 strings to UNICODE
 * and save the UNICODE string pointers in the handle
 * We also copy hints so that caller does not need to keep memory until the
 * callback.
 * return UV_OK if a callback will be made
 * return error code if validation fails
 *
 * To minimize allocation we calculate total size required,
 * and copy all structs and referenced strings into the one block.
 * Each size calculation is adjusted to avoid unaligned pointers.
 */
int uv_getaddrinfo(uv_loop_t* loop,
                   uv_getaddrinfo_t* handle,
                   uv_getaddrinfo_cb getaddrinfo_cb,
                   const char* node,
                   const char* service,
                   const struct addrinfo* hints) {
  int nodesize = 0;
  int servicesize = 0;
  int hintssize = 0;
  char* alloc_ptr = NULL;

  if (handle == NULL || getaddrinfo_cb == NULL ||
     (node == NULL && service == NULL)) {
    uv__set_sys_error(loop, WSAEINVAL);
    goto error;
  }

  uv_req_init(loop, (uv_req_t*)handle);

  handle->getaddrinfo_cb = getaddrinfo_cb;
  handle->res = NULL;
  handle->type = UV_GETADDRINFO;
  handle->loop = loop;

  /* calculate required memory size for all input values */
  if (node != NULL) {
    nodesize = ALIGNED_SIZE(uv_utf8_to_utf16(node, NULL, 0) * sizeof(wchar_t));
    if (nodesize == 0) {
      uv__set_sys_error(loop, GetLastError());
      goto error;
    }
  }

  if (service != NULL) {
    servicesize = ALIGNED_SIZE(uv_utf8_to_utf16(service, NULL, 0) *
                               sizeof(wchar_t));
    if (servicesize == 0) {
      uv__set_sys_error(loop, GetLastError());
      goto error;
    }
  }
  if (hints != NULL) {
    hintssize = ALIGNED_SIZE(sizeof(struct addrinfoW));
  }

  /* allocate memory for inputs, and partition it as needed */
  alloc_ptr = (char*)malloc(nodesize + servicesize + hintssize);
  if (!alloc_ptr) {
    uv__set_sys_error(loop, WSAENOBUFS);
    goto error;
  }

  /* save alloc_ptr now so we can free if error */
  handle->alloc = (void*)alloc_ptr;

  /* convert node string to UTF16 into allocated memory and save pointer in */
  /* handle */
  if (node != NULL) {
    handle->node = (wchar_t*)alloc_ptr;
    if (uv_utf8_to_utf16(node,
                         (wchar_t*) alloc_ptr,
                         nodesize / sizeof(wchar_t)) == 0) {
      uv__set_sys_error(loop, GetLastError());
      goto error;
    }
    alloc_ptr += nodesize;
  } else {
    handle->node = NULL;
  }

  /* convert service string to UTF16 into allocated memory and save pointer */
  /* in handle */
  if (service != NULL) {
    handle->service = (wchar_t*)alloc_ptr;
    if (uv_utf8_to_utf16(service,
                         (wchar_t*) alloc_ptr,
                         servicesize / sizeof(wchar_t)) == 0) {
      uv__set_sys_error(loop, GetLastError());
      goto error;
    }
    alloc_ptr += servicesize;
  } else {
    handle->service = NULL;
  }

  /* copy hints to allocated memory and save pointer in handle */
  if (hints != NULL) {
    handle->hints = (struct addrinfoW*)alloc_ptr;
    handle->hints->ai_family = hints->ai_family;
    handle->hints->ai_socktype = hints->ai_socktype;
    handle->hints->ai_protocol = hints->ai_protocol;
    handle->hints->ai_flags = hints->ai_flags;
    handle->hints->ai_addrlen = 0;
    handle->hints->ai_canonname = NULL;
    handle->hints->ai_addr = NULL;
    handle->hints->ai_next = NULL;
  } else {
    handle->hints = NULL;
  }

  /* init request for Post handling */
  uv_req_init(loop, &handle->getadddrinfo_req);
  handle->getadddrinfo_req.data = handle;
  handle->getadddrinfo_req.type = UV_GETADDRINFO_REQ;

  /* Ask thread to run. Treat this as a long operation */
  if (QueueUserWorkItem(&getaddrinfo_thread_proc,
                        handle,
                        WT_EXECUTELONGFUNCTION) == 0) {
    uv__set_sys_error(loop, GetLastError());
    goto error;
  }

  uv_ref(loop);

  return 0;

error:
  if (handle != NULL && handle->alloc != NULL) {
    free(handle->alloc);
  }
  return -1;
}
예제 #11
0
/*
 * Called from uv_run when complete. Call user specified callback
 * then free returned addrinfo
 * Returned addrinfo strings are converted from UTF-16 to UTF-8.
 *
 * To minimize allocation we calculate total size required,
 * and copy all structs and referenced strings into the one block.
 * Each size calculation is adjusted to avoid unaligned pointers.
 */
void uv_process_getaddrinfo_req(uv_loop_t* loop, uv_getaddrinfo_t* handle,
    uv_req_t* req) {
  int addrinfo_len = 0;
  int name_len = 0;
  size_t addrinfo_struct_len = ALIGNED_SIZE(sizeof(struct addrinfo));
  struct addrinfoW* addrinfow_ptr;
  struct addrinfo* addrinfo_ptr;
  char* alloc_ptr = NULL;
  char* cur_ptr = NULL;
  uv_err_code uv_ret;

  /* release input parameter memory */
  if (handle->alloc != NULL) {
    free(handle->alloc);
    handle->alloc = NULL;
  }

  uv_ret = uv_translate_eai_error(handle->retcode);
  if (handle->retcode == 0) {
    /* convert addrinfoW to addrinfo */
    /* first calculate required length */
    addrinfow_ptr = handle->res;
    while (addrinfow_ptr != NULL) {
      addrinfo_len += addrinfo_struct_len +
          ALIGNED_SIZE(addrinfow_ptr->ai_addrlen);
      if (addrinfow_ptr->ai_canonname != NULL) {
        name_len = uv_utf16_to_utf8(addrinfow_ptr->ai_canonname, -1, NULL, 0);
        if (name_len == 0) {
          uv_ret = uv_translate_sys_error(GetLastError());
          goto complete;
        }
        addrinfo_len += ALIGNED_SIZE(name_len);
      }
      addrinfow_ptr = addrinfow_ptr->ai_next;
    }

    /* allocate memory for addrinfo results */
    alloc_ptr = (char*)malloc(addrinfo_len);

    /* do conversions */
    if (alloc_ptr != NULL) {
      cur_ptr = alloc_ptr;
      addrinfow_ptr = handle->res;

      while (addrinfow_ptr != NULL) {
        /* copy addrinfo struct data */
        assert(cur_ptr + addrinfo_struct_len <= alloc_ptr + addrinfo_len);
        addrinfo_ptr = (struct addrinfo*)cur_ptr;
        addrinfo_ptr->ai_family = addrinfow_ptr->ai_family;
        addrinfo_ptr->ai_socktype = addrinfow_ptr->ai_socktype;
        addrinfo_ptr->ai_protocol = addrinfow_ptr->ai_protocol;
        addrinfo_ptr->ai_flags = addrinfow_ptr->ai_flags;
        addrinfo_ptr->ai_addrlen = addrinfow_ptr->ai_addrlen;
        addrinfo_ptr->ai_canonname = NULL;
        addrinfo_ptr->ai_addr = NULL;
        addrinfo_ptr->ai_next = NULL;

        cur_ptr += addrinfo_struct_len;

        /* copy sockaddr */
        if (addrinfo_ptr->ai_addrlen > 0) {
          assert(cur_ptr + addrinfo_ptr->ai_addrlen <=
                 alloc_ptr + addrinfo_len);
          memcpy(cur_ptr, addrinfow_ptr->ai_addr, addrinfo_ptr->ai_addrlen);
          addrinfo_ptr->ai_addr = (struct sockaddr*)cur_ptr;
          cur_ptr += ALIGNED_SIZE(addrinfo_ptr->ai_addrlen);
        }

        /* convert canonical name to UTF-8 */
        if (addrinfow_ptr->ai_canonname != NULL) {
          name_len = uv_utf16_to_utf8(addrinfow_ptr->ai_canonname,
                                      -1,
                                      NULL,
                                      0);
          assert(name_len > 0);
          assert(cur_ptr + name_len <= alloc_ptr + addrinfo_len);
          name_len = uv_utf16_to_utf8(addrinfow_ptr->ai_canonname,
                                      -1,
                                      cur_ptr,
                                      name_len);
          assert(name_len > 0);
          addrinfo_ptr->ai_canonname = cur_ptr;
          cur_ptr += ALIGNED_SIZE(name_len);
        }
        assert(cur_ptr <= alloc_ptr + addrinfo_len);

        /* set next ptr */
        addrinfow_ptr = addrinfow_ptr->ai_next;
        if (addrinfow_ptr != NULL) {
          addrinfo_ptr->ai_next = (struct addrinfo*)cur_ptr;
        }
      }
    } else {
      uv_ret = UV_ENOMEM;
    }

  }

  /* return memory to system */
  if (handle->res != NULL) {
    FreeAddrInfoW(handle->res);
    handle->res = NULL;
  }

complete:
  /* finally do callback with converted result */
  handle->getaddrinfo_cb(handle, uv_ret, (struct addrinfo*)alloc_ptr);

  uv_unref(loop);
}
예제 #12
0
/*
 * Called from uv_run when complete. Call user specified callback
 * then free returned addrinfo
 * Returned addrinfo strings are converted from UTF-16 to UTF-8.
 *
 * To minimize allocation we calculate total size required,
 * and copy all structs and referenced strings into the one block.
 * Each size calculation is adjusted to avoid unaligned pointers.
 */
static void uv__getaddrinfo_done(struct uv__work* w, int status) {
  uv_getaddrinfo_t* req;
  int addrinfo_len = 0;
  int name_len = 0;
  size_t addrinfo_struct_len = ALIGNED_SIZE(sizeof(struct addrinfo));
  struct addrinfoW* addrinfow_ptr;
  struct addrinfo* addrinfo_ptr;
  char* alloc_ptr = NULL;
  char* cur_ptr = NULL;

  req = container_of(w, uv_getaddrinfo_t, work_req);

  /* release input parameter memory */
  if (req->alloc != NULL) {
    free(req->alloc);
    req->alloc = NULL;
  }

  if (status == UV_ECANCELED) {
    assert(req->retcode == 0);
    req->retcode = UV_EAI_CANCELED;
    if (req->res != NULL) {
        FreeAddrInfoW(req->res);
        req->res = NULL;
    }
    goto complete;
  }

  if (req->retcode == 0) {
    /* convert addrinfoW to addrinfo */
    /* first calculate required length */
    addrinfow_ptr = req->res;
    while (addrinfow_ptr != NULL) {
      addrinfo_len += addrinfo_struct_len +
          ALIGNED_SIZE(addrinfow_ptr->ai_addrlen);
      if (addrinfow_ptr->ai_canonname != NULL) {
        name_len = uv_utf16_to_utf8(addrinfow_ptr->ai_canonname, -1, NULL, 0);
        if (name_len == 0) {
          req->retcode = uv_translate_sys_error(GetLastError());
          goto complete;
        }
        addrinfo_len += ALIGNED_SIZE(name_len);
      }
      addrinfow_ptr = addrinfow_ptr->ai_next;
    }

    /* allocate memory for addrinfo results */
    alloc_ptr = (char*)malloc(addrinfo_len);

    /* do conversions */
    if (alloc_ptr != NULL) {
      cur_ptr = alloc_ptr;
      addrinfow_ptr = req->res;

      while (addrinfow_ptr != NULL) {
        /* copy addrinfo struct data */
        assert(cur_ptr + addrinfo_struct_len <= alloc_ptr + addrinfo_len);
        addrinfo_ptr = (struct addrinfo*)cur_ptr;
        addrinfo_ptr->ai_family = addrinfow_ptr->ai_family;
        addrinfo_ptr->ai_socktype = addrinfow_ptr->ai_socktype;
        addrinfo_ptr->ai_protocol = addrinfow_ptr->ai_protocol;
        addrinfo_ptr->ai_flags = addrinfow_ptr->ai_flags;
        addrinfo_ptr->ai_addrlen = addrinfow_ptr->ai_addrlen;
        addrinfo_ptr->ai_canonname = NULL;
        addrinfo_ptr->ai_addr = NULL;
        addrinfo_ptr->ai_next = NULL;

        cur_ptr += addrinfo_struct_len;

        /* copy sockaddr */
        if (addrinfo_ptr->ai_addrlen > 0) {
          assert(cur_ptr + addrinfo_ptr->ai_addrlen <=
                 alloc_ptr + addrinfo_len);
          memcpy(cur_ptr, addrinfow_ptr->ai_addr, addrinfo_ptr->ai_addrlen);
          addrinfo_ptr->ai_addr = (struct sockaddr*)cur_ptr;
          cur_ptr += ALIGNED_SIZE(addrinfo_ptr->ai_addrlen);
        }

        /* convert canonical name to UTF-8 */
        if (addrinfow_ptr->ai_canonname != NULL) {
          name_len = uv_utf16_to_utf8(addrinfow_ptr->ai_canonname,
                                      -1,
                                      NULL,
                                      0);
          assert(name_len > 0);
          assert(cur_ptr + name_len <= alloc_ptr + addrinfo_len);
          name_len = uv_utf16_to_utf8(addrinfow_ptr->ai_canonname,
                                      -1,
                                      cur_ptr,
                                      name_len);
          assert(name_len > 0);
          addrinfo_ptr->ai_canonname = cur_ptr;
          cur_ptr += ALIGNED_SIZE(name_len);
        }
        assert(cur_ptr <= alloc_ptr + addrinfo_len);

        /* set next ptr */
        addrinfow_ptr = addrinfow_ptr->ai_next;
        if (addrinfow_ptr != NULL) {
          addrinfo_ptr->ai_next = (struct addrinfo*)cur_ptr;
        }
      }
    } else {
      req->retcode = UV_EAI_MEMORY;
    }
  }

  /* return memory to system */
  if (req->res != NULL) {
    FreeAddrInfoW(req->res);
    req->res = NULL;
  }

complete:
  uv__req_unregister(req->loop, req);

  /* finally do callback with converted result */
  req->getaddrinfo_cb(req, req->retcode, (struct addrinfo*)alloc_ptr);
}
예제 #13
0
/*
 * Entry point for getaddrinfo
 * we convert the UTF-8 strings to UNICODE
 * and save the UNICODE string pointers in the req
 * We also copy hints so that caller does not need to keep memory until the
 * callback.
 * return 0 if a callback will be made
 * return error code if validation fails
 *
 * To minimize allocation we calculate total size required,
 * and copy all structs and referenced strings into the one block.
 * Each size calculation is adjusted to avoid unaligned pointers.
 */
int uv_getaddrinfo(uv_loop_t* loop,
                   uv_getaddrinfo_t* req,
                   uv_getaddrinfo_cb getaddrinfo_cb,
                   const char* node,
                   const char* service,
                   const struct addrinfo* hints) {
  int nodesize = 0;
  int servicesize = 0;
  int hintssize = 0;
  char* alloc_ptr = NULL;
  int err;

  if (req == NULL || getaddrinfo_cb == NULL ||
     (node == NULL && service == NULL)) {
    err = WSAEINVAL;
    goto error;
  }

  uv_req_init(loop, (uv_req_t*)req);

  req->getaddrinfo_cb = getaddrinfo_cb;
  req->res = NULL;
  req->type = UV_GETADDRINFO;
  req->loop = loop;
  req->retcode = 0;

  /* calculate required memory size for all input values */
  if (node != NULL) {
    nodesize = ALIGNED_SIZE(uv_utf8_to_utf16(node, NULL, 0) * sizeof(WCHAR));
    if (nodesize == 0) {
      err = GetLastError();
      goto error;
    }
  }

  if (service != NULL) {
    servicesize = ALIGNED_SIZE(uv_utf8_to_utf16(service, NULL, 0) *
                               sizeof(WCHAR));
    if (servicesize == 0) {
      err = GetLastError();
      goto error;
    }
  }
  if (hints != NULL) {
    hintssize = ALIGNED_SIZE(sizeof(struct addrinfoW));
  }

  /* allocate memory for inputs, and partition it as needed */
  alloc_ptr = (char*)malloc(nodesize + servicesize + hintssize);
  if (!alloc_ptr) {
    err = WSAENOBUFS;
    goto error;
  }

  /* save alloc_ptr now so we can free if error */
  req->alloc = (void*)alloc_ptr;

  /* convert node string to UTF16 into allocated memory and save pointer in */
  /* the reques. */
  if (node != NULL) {
    req->node = (WCHAR*)alloc_ptr;
    if (uv_utf8_to_utf16(node,
                         (WCHAR*) alloc_ptr,
                         nodesize / sizeof(WCHAR)) == 0) {
      err = GetLastError();
      goto error;
    }
    alloc_ptr += nodesize;
  } else {
    req->node = NULL;
  }

  /* convert service string to UTF16 into allocated memory and save pointer */
  /* in the req. */
  if (service != NULL) {
    req->service = (WCHAR*)alloc_ptr;
    if (uv_utf8_to_utf16(service,
                         (WCHAR*) alloc_ptr,
                         servicesize / sizeof(WCHAR)) == 0) {
      err = GetLastError();
      goto error;
    }
    alloc_ptr += servicesize;
  } else {
    req->service = NULL;
  }

  /* copy hints to allocated memory and save pointer in req */
  if (hints != NULL) {
    req->hints = (struct addrinfoW*)alloc_ptr;
    req->hints->ai_family = hints->ai_family;
    req->hints->ai_socktype = hints->ai_socktype;
    req->hints->ai_protocol = hints->ai_protocol;
    req->hints->ai_flags = hints->ai_flags;
    req->hints->ai_addrlen = 0;
    req->hints->ai_canonname = NULL;
    req->hints->ai_addr = NULL;
    req->hints->ai_next = NULL;
  } else {
    req->hints = NULL;
  }

  uv__work_submit(loop,
                  &req->work_req,
                  uv__getaddrinfo_work,
                  uv__getaddrinfo_done);

  uv__req_register(loop, req);

  return 0;

error:
  if (req != NULL && req->alloc != NULL) {
    free(req->alloc);
  }
  return uv_translate_sys_error(err);
}
예제 #14
0
/*
 * Entry point for getaddrinfo
 * we convert the UTF-8 strings to UNICODE
 * and save the UNICODE string pointers in the req
 * We also copy hints so that caller does not need to keep memory until the
 * callback.
 * return 0 if a callback will be made
 * return error code if validation fails
 *
 * To minimize allocation we calculate total size required,
 * and copy all structs and referenced strings into the one block.
 * Each size calculation is adjusted to avoid unaligned pointers.
 */
int uv_getaddrinfo(uv_loop_t* loop,
                   uv_getaddrinfo_t* req,
                   uv_getaddrinfo_cb getaddrinfo_cb,
                   const char* node,
                   const char* service,
                   const struct addrinfo* hints) {
  int nodesize = 0;
  int servicesize = 0;
  int hintssize = 0;
  char* alloc_ptr = NULL;
  int err;

  if (req == NULL || (node == NULL && service == NULL)) {
    return UV_EINVAL;
  }

  UV_REQ_INIT(req, UV_GETADDRINFO);
  req->getaddrinfo_cb = getaddrinfo_cb;
  req->addrinfo = NULL;
  req->loop = loop;
  req->retcode = 0;

  /* calculate required memory size for all input values */
  if (node != NULL) {
    nodesize = ALIGNED_SIZE(MultiByteToWideChar(CP_UTF8, 0, node, -1, NULL, 0) *
                            sizeof(WCHAR));
    if (nodesize == 0) {
      err = GetLastError();
      goto error;
    }
  }

  if (service != NULL) {
    servicesize = ALIGNED_SIZE(MultiByteToWideChar(CP_UTF8,
                                                   0,
                                                   service,
                                                   -1,
                                                   NULL,
                                                   0) *
                               sizeof(WCHAR));
    if (servicesize == 0) {
      err = GetLastError();
      goto error;
    }
  }
  if (hints != NULL) {
    hintssize = ALIGNED_SIZE(sizeof(struct addrinfoW));
  }

  /* allocate memory for inputs, and partition it as needed */
  alloc_ptr = (char*)uv__malloc(nodesize + servicesize + hintssize);
  if (!alloc_ptr) {
    err = WSAENOBUFS;
    goto error;
  }

  /* save alloc_ptr now so we can free if error */
  req->alloc = (void*)alloc_ptr;

  /* Convert node string to UTF16 into allocated memory and save pointer in the
   * request. */
  if (node != NULL) {
    req->node = (WCHAR*)alloc_ptr;
    if (MultiByteToWideChar(CP_UTF8,
                            0,
                            node,
                            -1,
                            (WCHAR*) alloc_ptr,
                            nodesize / sizeof(WCHAR)) == 0) {
      err = GetLastError();
      goto error;
    }
    alloc_ptr += nodesize;
  } else {
    req->node = NULL;
  }

  /* Convert service string to UTF16 into allocated memory and save pointer in
   * the req. */
  if (service != NULL) {
    req->service = (WCHAR*)alloc_ptr;
    if (MultiByteToWideChar(CP_UTF8,
                            0,
                            service,
                            -1,
                            (WCHAR*) alloc_ptr,
                            servicesize / sizeof(WCHAR)) == 0) {
      err = GetLastError();
      goto error;
    }
    alloc_ptr += servicesize;
  } else {
    req->service = NULL;
  }

  /* copy hints to allocated memory and save pointer in req */
  if (hints != NULL) {
    req->addrinfow = (struct addrinfoW*)alloc_ptr;
    req->addrinfow->ai_family = hints->ai_family;
    req->addrinfow->ai_socktype = hints->ai_socktype;
    req->addrinfow->ai_protocol = hints->ai_protocol;
    req->addrinfow->ai_flags = hints->ai_flags;
    req->addrinfow->ai_addrlen = 0;
    req->addrinfow->ai_canonname = NULL;
    req->addrinfow->ai_addr = NULL;
    req->addrinfow->ai_next = NULL;
  } else {
    req->addrinfow = NULL;
  }

  uv__req_register(loop, req);

  if (getaddrinfo_cb) {
    uv__work_submit(loop,
                    &req->work_req,
                    UV__WORK_SLOW_IO,
                    uv__getaddrinfo_work,
                    uv__getaddrinfo_done);
    return 0;
  } else {
    uv__getaddrinfo_work(&req->work_req);
    uv__getaddrinfo_done(&req->work_req, 0);
    return req->retcode;
  }

error:
  if (req != NULL) {
    uv__free(req->alloc);
    req->alloc = NULL;
  }
  return uv_translate_sys_error(err);
}
예제 #15
0
/************************************************************************
   Description: 
      This function reads the RDA adaptation data block.	

   Return: 
      Pointer to adaptation data structure on success or NULL on error.

************************************************************************/
void* ORPGRDA_ADPT_read( ){

   int status;

   /* First check to see if ORDA adaptation data LB notification 
      registered. */

   if (!ORDA_adpt_registered) {

      status = ORPGDA_UN_register( ORPGDAT_RDA_ADAPT_DATA,
				   ORPGDAT_RDA_ADAPT_MSG_ID, ORPGRDA_ADPT_lb_notify);

      if( status != LB_SUCCESS ){

         LE_send_msg( GL_ERROR,
            "ORPGRDA_ADPT: UN_register(ORPGDAT_RDA_ADAPT_DATA) failed (ret %d)", 
            status);
         return( (void *) NULL );

      }

      ORDA_adpt_registered = 1;

   }

   /* If adaptation data previously read, free buffer now. */
   if( ORDA_adpt_msg != NULL ){

      free( ORDA_adpt_msg );
      ORDA_adpt_msg = NULL;

   }

   /* Read the ORDA adaptation data. */
   status = ORPGDA_read( ORPGDAT_RDA_ADAPT_DATA, (char *) &ORDA_adpt_msg,
			 LB_ALLOC_BUF, ORPGDAT_RDA_ADAPT_MSG_ID);

   if( (status < 0) || (status != ALIGNED_SIZE(sizeof(ORDA_adpt_data_msg_t))) ) {

      LE_send_msg( GL_INFO, "ORPGRDA: ORPGDAT_RDA_ADAPT_DATA read failed: %d\n",
                   status );

      return( (void *) ORDA_adpt );

   }

   /* One time RDA adaptation data buffer allocation. */
   if( ORDA_adpt == NULL ){

      ORDA_adpt = malloc( ALIGNED_SIZE(sizeof(ORDA_adpt_data_t) ) );
      if( ORDA_adpt == NULL ){

         LE_send_msg( GL_ERROR, "malloc Failed for %d Bytes.\n",
                      ALIGNED_SIZE( sizeof(ORDA_adpt_data_t) ) );
         return( (void *) ORDA_adpt );

      }

   }

   memcpy( ORDA_adpt, &ORDA_adpt_msg->rda_adapt, sizeof(ORDA_adpt_data_t) );

   ORDA_adpt_updated = 1;

   return( (void *) ORDA_adpt ); 

/* End of ORPGRDA_ADPT_read() */
}
예제 #16
0
파일: orpgrat.c 프로젝트: likev/CodeOrpgPub
/************************************************************************
 *  Description:  The following module reads the RDA alarms table	*
 *  		  from the RDA alarms definition lb.  The size of	*
 *		  the message containing the data is determined first 	*
 *  		  and memory is dynamically allocated to store the	*
 *  		  message in memory.  A pointer is kept to the start of	*
 *		  the data.						*
 *									*
 *  Return:	  On success 0 is returned, otherwise 			*
 *		  ORPGRAT_DATA_NOT_FOUND is retuned.			*
 *									*
 ************************************************************************/
int ORPGRAT_read_rda_alarms_tbl( int table ){

   int status;
   RDA_alarm_entry_t *alarm;
   int offset;
   int i;

   Alarm_tbl_num[table]  = 0;

   /* Initialize lookup table for mapping alarm code to table index. */
   for( i = 0; i <= ORPGRAT_MAX_RDA_ALARMS; i++ )
      Code_to_index[table][i] = ORPGRAT_DATA_NOT_FOUND;

   /* Make a pass through the RDA alarms message and create a lookup table
      to the start of each alarm definition.  We maintain lookup tables 
      referenced by alarm code.	*/

   /* Check to see if the table has already been initialized.  If so, 
      free up the memory associated with it.	*/
   Alarm_tbl_num[table]  = 0;

   if( Alarm_tbl[table] != (char *) NULL )
      free (Alarm_tbl[table]);


   /* Now read the RDA alarms table into the buffer just created.	*/ 
   if( table == ORPGRAT_RDA_TABLE )
      status = ORPGDA_read( ORPGDAT_RDA_ALARMS_TBL, &Alarm_tbl[table], 
                            LB_ALLOC_BUF, ORPGRAT_ALARMS_TBL_MSG_ID );

   else
      status = ORPGDA_read( ORPGDAT_RDA_ALARMS_TBL, &Alarm_tbl[table], 
                               LB_ALLOC_BUF, ORPGRAT_ORDA_ALARMS_TBL_MSG_ID);
   if( status <= 0 ){

      LE_send_msg( GL_INPUT, "ORPGDA_read of Alarm Table (%d) Failed (%d)\n",
		   table, status);
      return (ORPGRAT_DATA_NOT_FOUND);

   }

   offset = 0;
   while (offset < status) {

      short code;

      if( Alarm_tbl_num[table] > ORPGRAT_MAX_RDA_ALARMS ){

         LE_send_msg( GL_INPUT, "Too Many Alarms (%d)\n", Alarm_tbl_num);
         return (ORPGRAT_ERROR);

      }

      /* Alarm_tbl_ptr contains the offset, in bytes, to the start of each 
         alarm table record.	*/
      Alarm_tbl_ptr[table][Alarm_tbl_num[table]] = offset;

      /* Cast the current buffer pointer to the main alarms table structure.
         The size of the record can be determined from the size element
         in the main structure.  The start of the next record is the current	
	 offset plus the current record size. */
      alarm = (RDA_alarm_entry_t *) (Alarm_tbl[table]+offset);
      offset += ALIGNED_SIZE(sizeof(RDA_alarm_entry_t));

      /* Update the reference record by alarm code lookup table */
      code = alarm->code;
      if( code <= ORPGRAT_MAX_RDA_ALARMS ){

         if( Code_to_index[table][code] < 0 )
            Code_to_index[table][code] = Alarm_tbl_num[table];

         else if( code != 0 ){

            /* non-zero duplicated Alarm Code */
            LE_send_msg( GL_INPUT, "Duplicated Alarm Code (%d) in RDA Alarms Table\n",
                         code );

         }

      }

      Alarm_tbl_num[table]++;

   }

   Need_update[table] = 0;

   return 0;

/* ORPGRAT_read_rda_alarms_tbl() */
}
예제 #17
0
void MPI_housekeep () {
    static time_t last_tm = 0;
    time_t tm;
    int updated, n_nodes, i;
    Node_attribute_t *nodes;

    tm = MISC_systime (NULL);
    if (tm > last_tm + NDS_CHECK_TIME) {	/* check nds */
	Start_all_nds ();
	last_tm = tm;
    }

    /* kills processes on unassigned node  */
    if (Unassigned_rm_time == 0)
	Unassigned_rm_time = tm;
    if (tm >= Unassigned_rm_time + 10) {	/* remove unassigned prcs */
	Unassigned_proc_t *usp = (Unassigned_proc_t *)Unassigned_prcs;
	for (i = 0; i < N_unassigned_prcs; i++) {
	    LE_send_msg (GL_INFO, "Kill unassignedified task %s (pid %d) on %s",
			usp->prc->name, usp->pid, usp->node->node);
	    RSS_kill (usp->node->hname, usp->pid , SIGKILL);
	    usp++;
	}
	Unassigned_prcs = STR_reset (Unassigned_prcs, 128);
	N_unassigned_prcs = 0;
	Unassigned_rm_time = tm;
    }

    updated = 0;
    n_nodes = MHR_all_hosts (&nodes);
    for (i = 0; i < n_nodes; i++) {
	if (nodes[i].proc_table_updated) {
	    nodes[i].proc_table_updated = 0;
	    Read_status_info (NDS_PROC_TABLE, nodes + i);
	    updated = 1;
	}
    }
    if (updated)
	Set_task_fail_alarm ();

    if (updated) {		/* publish process table */
	Mrpg_tat_entry_t *prc;
	int first, cnt, ret, name_size, off;
	Mrpg_process_table_t *pt;
	char *buf;

	cnt = 0;
	name_size = 0;
	first = 1;
	while ((prc = MRT_get_next_task (first)) != NULL) {
	    int status;
	    first = 0;
	    status = prc->status;
	    if (status == MRPG_PS_ACTIVE || status == MRPG_PS_FAILED) {
		cnt++;
		name_size += strlen (prc->name) + 1 + 4;
	    }
	}
	buf = (char *)malloc (cnt * sizeof (Mrpg_process_table_t) + name_size);
	if (buf == NULL) {
	    LE_send_msg (GL_ERROR, "Malloc failed");
	    return;
	}
	off = 0;
	first = 1;
	while ((prc = MRT_get_next_task (first)) != NULL) {
	    first = 0;
	    pt = (Mrpg_process_table_t *)(buf + off);
	    if (prc->status == MRPG_PS_ACTIVE || 
					prc->status == MRPG_PS_FAILED) {
		pt->instance = prc->instance;
		pt->pid = prc->pid;
		pt->status = prc->status;
		pt->name_off = sizeof (Mrpg_process_table_t);
		strcpy ((char *)pt + sizeof (Mrpg_process_table_t), prc->name);
		pt->size = ALIGNED_SIZE (sizeof (Mrpg_process_table_t) + 
						strlen (prc->name) + 1);
		off += pt->size;
	    }
	}
	ret = ORPGDA_write (ORPGDAT_TASK_STATUS, buf, off, MRPG_PT_MSGID);
	free (buf);
	if (ret < 0) {
	    LE_send_msg (GL_ERROR, 
			"ORPGDA_write MRPG_PT_MSGID failed (ret %d)", ret);
	    return;
	}
    }
}
예제 #18
0
static void Pack_unsent_message_no_seek (Link_struct *link) {
    static char *msg_sizes = NULL;
    int cnt, len0, data_size, cmp_h_s;
    Req_struct req;
    char *p0, *msgs;

    if (link->pack_time > 1) {
	int cr_ms;
	time_t cr_tm = MISC_systime (&cr_ms);
	if (link->pack_cnt < link->max_pack &&
	    ((double)cr_tm + .001 * cr_ms) - link->pack_st_time < 
						    .001 * link->pack_time)
	    return;
    }

    if (link->n_added_bytes > 0) {
	LE_send_msg (GL_ERROR, 
		"n_added_bytes > 0 and pack_time > 0 not supported");
	CM_terminate ();
    }

    Rm_done_requests (link);
    if (link->n_reqs >= MAX_N_REQS) {
	LE_send_msg (GL_ERROR, "Too many unprocessed requests");
	return;
    }

    cmp_h_s = CMPR_comp_hd_size () + sizeof (int);
    link->pack_buf = STR_reset (link->pack_buf, 40000);
    link->pack_buf = STR_append (link->pack_buf, NULL, 
				sizeof (CM_req_struct) + cmp_h_s);
    msg_sizes = STR_reset (msg_sizes, 128);
    data_size = 0;
    cnt = len0 = 0;
    p0 = NULL;
    msgs = link->saved_msgs;
    while (cnt < link->pack_cnt) {
	int len, tmp;
	char *p;

	p = msgs + sizeof (int);
	len = *((int *)msgs);
	msgs = msgs + ALIGNED_SIZE (len) + sizeof (int);

	len -= sizeof (CM_req_struct);
	if (cnt == 0)
	    link->pack_buf = STR_append (link->pack_buf, NULL, len);
	else
	    link->pack_buf = STR_append (link->pack_buf, 
					p + sizeof (CM_req_struct), len);
	data_size += len;
	tmp = htonl (len);
	msg_sizes = STR_append (msg_sizes, (char *)&tmp, sizeof (int));

	memcpy (&(req.type), p, sizeof (CM_req_struct));
	if (cnt == 0) {
	    p0 = p;
	    len0 = data_size;
	}

	cnt++;
    }

    if (cnt == 1) {
	memcpy (link->pack_buf, p0, data_size + sizeof (CM_req_struct));
	req.state = CM_NEW;
    }
    else {
	memcpy (link->pack_buf + sizeof (CM_req_struct) + cmp_h_s, 
					p0 + sizeof (CM_req_struct), len0);
	*((int *)(link->pack_buf + 
	    sizeof (CM_req_struct) + cmp_h_s - sizeof (int))) = htonl (cnt);
	link->pack_buf = STR_append (link->pack_buf, msg_sizes, 
						    cnt * sizeof (int));
	data_size += cnt * sizeof (int) + cmp_h_s;
	req.data_size = data_size;
	memcpy (link->pack_buf, &(req.type), sizeof (CM_req_struct));
	req.state = CM_NEW_PACKED;
    }
    req.data = link->pack_buf;
    req.msg_id = LB_ANY;

    memcpy (&(link->req[(link->st_ind + link->n_reqs) % MAX_N_REQS]), 
					&req, sizeof (Req_struct));
    link->n_reqs++;
    LE_send_msg (LE_VL3, "    %d messages packed", link->pack_cnt);

    link->pack_cnt = 0;
    return;
}
예제 #19
0
/******************************************************************

   Description:
      Converts message 31, major version 2 to major version 1. 

   Inputs:
      rda_msg - RDA message 31.
      hdr_off - offset from start of message to RDA/RPG Message 
                Header.

   Returns:
      Pointer to radial message on success, NULL on failure.

******************************************************************/
static char* Message_31_convert( char *radar_data, int hdr_off ){

   static int buf_size = 0;
   static char *buf = NULL;

   /* Set pointers that are at fixed locations. */
   char *rda_msg = radar_data + hdr_off;
   Generic_basedata_t *gbd = (Generic_basedata_t *) rda_msg;
   RDA_RPG_message_header_t *msg_hd = (RDA_RPG_message_header_t *) &gbd->msg_hdr;
   Generic_basedata_header_t *gbhd = (Generic_basedata_header_t *) &gbd->base;

   /* The following pointer are set when parsing the radial. */
   Generic_vol_t *vol_hd = NULL;
   Generic_elev_t *elv_hd = NULL;
   Generic_rad_t *rad_hd = NULL;
   Generic_moment_t *refhd = NULL, *velhd = NULL, *spwhd = NULL;
   Generic_moment_t *zdrhd = NULL, *rhohd = NULL, *phihd = NULL;

   /* The sizes are set when parsing the radial. */
   int vol_hd_size = 0, elv_hd_size = 0, rad_hd_size = 0;
   int ref_size = 0, vel_size = 0, spw_size = 0;
   int zdr_size = 0, rho_size = 0, phi_size = 0;
   int offset = 0;

   /* Number of blocks. */
   int no_of_datum = gbhd->no_of_datum;
   int size = msg_hd->size*sizeof(short) + hdr_off;
   int i, cnt = 0;

   /* Allocate temporary buffer. */
   if( (size > buf_size) 
            ||
       (buf == NULL) ){

      if( buf != NULL )
         free(buf);

      buf = calloc( size + 1000, 1 );
      if( buf == NULL ){

         LE_send_msg( GL_INFO, "calloc Failed for %d bytes\n", size );
         return(radar_data);

      }

      buf_size = size + 1000;

   }

   /* Initialize the size of the radial.  Note: We include
      the number of bytes needed for the data block offsets. */
   size = ALIGNED_SIZE( sizeof(Generic_basedata_header_t) + 
                        sizeof(RDA_RPG_message_header_t) + 
                        hdr_off +
                        no_of_datum*sizeof(int) );

   /* Copy the RDA/RPG message header and standard radial header to 
      temporary buffer. */
   memcpy( buf, radar_data, size );

   /* Parse the radial message. */
   for( i = 0; i < no_of_datum; i++ ){

      Generic_any_t *data_block = NULL;
      char type[5];

      offset = gbhd->data[i];

      data_block = (Generic_any_t *) (rda_msg + sizeof(RDA_RPG_message_header_t) + offset );
      memset( type, 0, 5 );
      memcpy( type, data_block->name, 4 );

      if( strstr( type, "RVOL" ) != NULL ){

         vol_hd = (Generic_vol_t *) data_block;
         vol_hd_size = ALIGNED_SIZE( sizeof(Generic_vol_t) );

      }
      else if( strstr( type, "RELV" ) != NULL ){

         elv_hd = (Generic_elev_t *) data_block;
         elv_hd_size = ALIGNED_SIZE( sizeof(Generic_elev_t) );

      }
      else if( strstr( type, "RRAD" ) != NULL ){

         rad_hd = (Generic_rad_t *) data_block;
         rad_hd_size = ALIGNED_SIZE( sizeof(Generic_rad_t) );

      }
      else if( strstr( type, "DREF" ) != NULL ){

         refhd = (Generic_moment_t *) data_block;
         ref_size = sizeof( Generic_moment_t);
         if( refhd->data_word_size == 8 )
            ref_size += refhd->no_of_gates*sizeof(unsigned char);

         else if( refhd->data_word_size == 16 )
            ref_size += refhd->no_of_gates*sizeof(unsigned short);

         else
            ref_size += refhd->no_of_gates*sizeof(unsigned char);

      }
      else if( strstr( type, "DVEL" ) != NULL ){

         velhd = (Generic_moment_t *) data_block;
         vel_size = ALIGNED_SIZE( sizeof( Generic_moment_t) );
         if( velhd->data_word_size == 8 )
            vel_size += velhd->no_of_gates*sizeof(unsigned char);

         else if( velhd->data_word_size == 16 )
            vel_size += velhd->no_of_gates*sizeof(unsigned short);

         else
            vel_size += velhd->no_of_gates*sizeof(unsigned char);

      }
      else if( strstr( type, "DSW" ) != NULL ){

         spwhd = (Generic_moment_t *) data_block;
         spw_size = ALIGNED_SIZE( sizeof( Generic_moment_t) );
         if( spwhd->data_word_size == 8 )
            spw_size += spwhd->no_of_gates*sizeof(unsigned char);

         else if( spwhd->data_word_size == 16 )
            spw_size += spwhd->no_of_gates*sizeof(unsigned short);

         else
            spw_size += spwhd->no_of_gates*sizeof(unsigned char);

      }
      else if( strstr( type, "DZDR" ) != NULL ){

         zdrhd = (Generic_moment_t *) data_block;
         zdr_size = ALIGNED_SIZE( sizeof( Generic_moment_t) );
         if( zdrhd->data_word_size == 8 )
            zdr_size += zdrhd->no_of_gates*sizeof(unsigned char);

         else if( zdrhd->data_word_size == 16 )
            zdr_size += zdrhd->no_of_gates*sizeof(unsigned short);

         else
            zdr_size += zdrhd->no_of_gates*sizeof(unsigned char);

      }
      else if( strstr( type, "DRHO" ) != NULL ){

         rhohd = (Generic_moment_t *) data_block;
         rho_size = ALIGNED_SIZE( sizeof( Generic_moment_t) );
         if( rhohd->data_word_size == 8 )
            rho_size += ALIGNED_SIZE( rhohd->no_of_gates*sizeof(unsigned char));

         else if( rhohd->data_word_size == 16 )
            rho_size += rhohd->no_of_gates*sizeof(unsigned short);

         else
            rho_size += rhohd->no_of_gates*sizeof(unsigned char);

      }
      else if( strstr( type, "DPHI" ) != NULL ){

         phihd = (Generic_moment_t *) data_block;
         phi_size = ALIGNED_SIZE( sizeof( Generic_moment_t) );
         if( phihd->data_word_size == 8 )
            phi_size += phihd->no_of_gates*sizeof(unsigned char);

         else if( phihd->data_word_size == 16 )
            phi_size += phihd->no_of_gates*sizeof(unsigned short);

         else
            phi_size += phihd->no_of_gates*sizeof(unsigned short);

      }

   }

   /* Reset pointers. */
   gbd = (Generic_basedata_t *) (buf + hdr_off);
   msg_hd = (RDA_RPG_message_header_t *) &gbd->msg_hdr;
   gbhd = (Generic_basedata_header_t *) &gbd->base;

   /* Copy the Volume Header Block. */
   memcpy( (buf + size), vol_hd, vol_hd_size );

   /* Change the version. */
   vol_hd = (Generic_vol_t *) (buf + size);
   vol_hd->major_version = 1;
   vol_hd->minor_version = 0;

   /* Offset to Volume Header Block.  This is the size of the 
      Generic_basedata_header plus 4 bytes for every data block. */
   gbhd->data[0] = ALIGNED_SIZE( sizeof(Generic_basedata_header_t) + 
                                 no_of_datum*sizeof(int) );

   /* Increment the total size. */
   size += vol_hd_size;

   /* Copy the Elevation Header Block and increment the total size. */
   memcpy( (buf + size), elv_hd, elv_hd_size );
   size += elv_hd_size;

   /* Offset to Elevation Header Block. */
   gbhd->data[1] = gbhd->data[0] + vol_hd_size;

   /* Copy the Radial Header Block and increment the total size. */
   memcpy( (buf + size), rad_hd, rad_hd_size );

   /* Change the block size. */
   rad_hd = (Generic_rad_t *) (buf + size);
   rad_hd->len = rad_hd_size;

   /* Increment the total size. */
   size += rad_hd_size;

   /* Offset to Radial Header Block. */
   gbhd->data[2] = gbhd->data[1] + elv_hd_size;


   /* Fill out the moments */
   cnt = 3;
   offset = rad_hd_size;

   /* Did radial have reflectivity? */
   if( refhd != NULL ){

      memcpy( (buf + size), refhd, ref_size );

      ref_size = ALIGNED_SIZE( ref_size );
      size += ref_size;

      /* Set the data block offset. */
      gbhd->data[cnt] = gbhd->data[cnt-1] + offset;
      offset = ref_size;

      /* Increment the number of blocks. */
      cnt++;

   }

   /* Did radial have velocity? */
   if( velhd != NULL ){

      memcpy( (buf + size), velhd, vel_size );

      vel_size = ALIGNED_SIZE( vel_size );
      size += vel_size;

      /* Set the data block offset. */
      gbhd->data[cnt] = gbhd->data[cnt-1] + offset;
      offset = vel_size;

      /* Increment the number of blocks. */
      cnt++;

   }

   /* Did radial have spectrum width? */
   if( spwhd != NULL ){

      memcpy( (buf + size), spwhd, spw_size );

      spw_size = ALIGNED_SIZE( spw_size );
      size += spw_size;

      /* Set the data block offset. */
      gbhd->data[cnt] = gbhd->data[cnt-1] + offset;
      offset = spw_size;

      /* Increment the number of blocks. */
      cnt++;

   }

   /* Did radial have differential reflectivity? */
   if( zdrhd != NULL ){

      memcpy( (buf + size), zdrhd, zdr_size );

      zdr_size = ALIGNED_SIZE( zdr_size );
      size += zdr_size;

      gbhd->data[cnt] = gbhd->data[cnt-1] + offset;
      offset = zdr_size;

      /* Increment the number of blocks. */
      cnt++;

   }

   /* Did radial have differential phase? */
   if( phihd != NULL ){

      memcpy( (buf + size), phihd, phi_size );
 
      phi_size = ALIGNED_SIZE( phi_size );
      size += phi_size;

      gbhd->data[cnt] = gbhd->data[cnt-1] + offset;
      offset = phi_size;

      /* Increment the number of blocks. */
      cnt++;

   }

   /* Did radial have correlation coefficient? */
   if( rhohd != NULL ){

      memcpy( (buf + size), rhohd, rho_size );

      rho_size = ALIGNED_SIZE( rho_size );
      size += rho_size;

      gbhd->data[cnt] = gbhd->data[cnt-1] + offset;
      offset = rho_size;

      /* Increment the number of blocks. */
      cnt++;

   }

   /* Update the size fields in the various headers. */
   gbhd->radial_length = size - hdr_off;
   msg_hd->size = (size - hdr_off + 1)/sizeof(short);

   /* Set the number of datum and data offsets. */ 
   gbhd->no_of_datum = cnt;

   /* Verify cnt matches no_of_datum. */
   if( cnt != no_of_datum ){

      LE_send_msg( GL_INFO, "cnt (%d) != no_of_datum (%d)\n",
                   cnt, no_of_datum );
      return(radar_data);

   }

LE_send_msg( 0, "Leaving Message 31\n" );
   return (buf);

/* End of Message_31_convert(). */
}
예제 #20
0
int MHR_publish_node_info () {
    Node_attribute_t *n;
    int size, off, i, ret, k;
    char *buf, *cp;
    Mrpg_node_t *ni;

    size = 0;
    for (i = 0; i < N_nodes; i++) {	/* estimate the buffer size needed */
	n = Nodes + i;
	size += strlen (n->node) + strlen (n->hname) + strlen (n->orpgdir) + 
			strlen (n->workdir) + strlen (n->logdir) + 8;
	for (k = 0; k < n->n_tasks; k++)
	    size += strlen (n->tasks[k]) + 1;
	size += sizeof (Mrpg_node_t) + ALIGNED_LENGTH;
    }

    buf = malloc (size);
    if (buf == NULL) {
	LE_send_msg (GL_ERROR, "malloc failed\n");
	return (-1);
    }

    size = 0;
    for (i = 0; i < N_nodes; i++) {	/* create the message */
	off = 0;
	cp = buf + size;
	n = Nodes + i;
	ni = (Mrpg_node_t *)cp;
	ni->ip = n->ip;
	ni->is_local = n->is_local;
	ni->default_node = n->default_node;
	ni->is_bigendian = n->is_bigendian;
	ni->is_connected = n->is_connected;
	off += sizeof (Mrpg_node_t);

	ni->host_off = off;
	strcpy (cp + off, n->hname);
	off += strlen (cp + off) + 1;
	ni->node_off = off;
	strcpy (cp + off, n->node);
	off += strlen (cp + off) + 1;
	ni->orpgdir_off = off;
	strcpy (cp + off, n->orpgdir);
	off += strlen (cp + off) + 1;
	ni->workdir_off = off;
	strcpy (cp + off, n->workdir);
	off += strlen (cp + off) + 1;
	ni->logdir_off = off;
	strcpy (cp + off, n->logdir);
	off += strlen (cp + off) + 1;
	if (n->n_tasks == 0)
	    ni->tasks_off = 0;
	else {
	    ni->tasks_off = off;
	    for (k = 0; k < n->n_tasks; k++) {
		sprintf (cp + off, "%s ", n->tasks[k]);
		off += strlen (cp + off);
	    }
	    cp[off - 1] = '\0';
	}
	off = ALIGNED_SIZE (off);
	ni->size = off;
	size += off;
    }

    ret = ORPGDA_write (ORPGDAT_TASK_STATUS, buf, size, MRPG_RPG_NODE_MSGID);
    if (ret != size) {
	LE_send_msg (GL_ERROR, 
		"ORPGDA_write MRPG_RPG_NODE_MSGID failed (%d)\n", ret);
	free (buf);
	return (-1);
    }
    free (buf);
    return (0);
}
예제 #21
0
/**************************************************************************
   Description:
      Initialize the Task Attribute Table

   Input:
      ttcf_fname - task table configuration file filename

   Output:

   Returns: 0 upon success; otherwise -1

   Notes:

 **************************************************************************/
int ITI_ATTR_init_table( const char *ttcf_fname, char **tat_dir ) {

    size_t byte_cnt ;
    Orpgtat_mem_ary_t mem_ary ;
    Orpgtat_dir_entry_t dir_entry;
    int retval;
    int retval2;

    /* Read Task Attribute Table entries from the Task Table Configuration
       File.  Write the "public" array of Task Attribute Table entries
       into ORPGDAT_RPG_INFO/ORPGINFO_TAT_MSGID. */
    mem_ary.size_bytes = 0;
    mem_ary.ptr = NULL;

    /* Read array of Task Attribute Table entries from the Task
       Table Configuration File. */
    retval = ORPGTAT_read_ascii_tbl( &mem_ary, (const char *) ttcf_fname );

    if( retval != 0 ) {

        LE_send_msg( GL_ERROR, "ORPGTAT_read_ascii_tbl() returned %d",
                     retval );
        return(-1);

    }

    /* Write the LB messages ... */
    byte_cnt = 0 ;
    while( byte_cnt < mem_ary.size_bytes ) {

        Orpgtat_entry_t *entry_p;
        LB_id_t msg_id;
        int i;

        entry_p = (Orpgtat_entry_t *) ((char *) mem_ary.ptr + byte_cnt) ;

        /* Check for duplicate ..... */
        if( *tat_dir != NULL ) {

            int length;

            length = STR_size( *tat_dir ) / ALIGNED_SIZE(sizeof(Orpgtat_dir_entry_t));
            msg_id = ITI_check_for_duplicate( (Orpgtat_dir_entry_t *) *tat_dir, length, entry_p );

        }
        else
            msg_id = LB_NEXT;

        /* The return value of the previous function will either be LB_NEXT
           or the message ID of a duplicate entry. */
        retval2 = ORPGDA_write( ORPGDAT_TAT, (char *) entry_p,
                                entry_p->entry_size, msg_id );
        if( retval2 != (int) entry_p->entry_size ) {

            LE_send_msg( GL_ERROR, "ORPGDA_write(ORPGDAT_TAT) failed (%d)", retval2 );
            retval = -1;

        }
        else {


            dir_entry.msg_id = ORPGDA_previous_msgid( ORPGDAT_TAT );
            strcpy( (char *) &dir_entry.task_name[0], &entry_p->task_name[0] );
            strcpy( (char *) &dir_entry.file_name[0], &entry_p->file_name[0] );
            *tat_dir = STR_append( *tat_dir, (char *) &dir_entry,
                                   sizeof(Orpgtat_dir_entry_t) );

            if( Verbose ) {

                LE_send_msg( GL_INFO, "TAT Entry For Task Name: %s, Process Name: %s",
                             entry_p->task_name, entry_p->file_name );
                LE_send_msg( GL_INFO, "--->type: %d, data_stream: %d\n",
                             entry_p->type, entry_p->data_stream );
                if( entry_p->type & ORPGTAT_TYPE_ALLOW_SUPPL_SCANS )
                    LE_send_msg( GL_INFO, "------>Allow Supplemental Scans\n" );
                if( (entry_p->num_input_dataids > 0) || (entry_p->num_output_dataids > 0) )
                    LE_send_msg( GL_INFO, "---># inputs: %d, # outputs: %d\n",
                                 entry_p->num_input_dataids, entry_p->num_output_dataids );
                if( entry_p->num_input_dataids > 0 ) {

                    int *int_p = (int *) ((char *) entry_p + entry_p->input_data);
                    char *char_p = ((char *) entry_p + entry_p->input_names);

                    for( i = 0; i < entry_p->num_input_dataids; i++ ) {

                        LE_send_msg( GL_INFO, "------>INPUT: data ID: %d, data name: %s\n",
                                     *int_p, char_p );
                        int_p++;
                        char_p += (strlen(char_p) + 1);

                    }

                }

                if( entry_p->num_output_dataids > 0 ) {

                    int *int_p = (int *) ((char *) entry_p + entry_p->output_data);
                    char *char_p = ((char *) entry_p + entry_p->output_names);

                    for( i = 0; i < entry_p->num_output_dataids; i++ ) {

                        LE_send_msg( GL_INFO, "------>OUTPUT: data ID: %d, data name: %s\n",
                                     *int_p, char_p );
                        int_p++;
                        char_p += (strlen(char_p) + 1);

                    }

                }

                LE_send_msg( GL_INFO, "\n" );

            }

        }

        byte_cnt += entry_p->entry_size ;

    } /*endwhile stepping through the TAT memory array*/

    free(mem_ary.ptr) ;
    mem_ary.ptr = NULL ;
    mem_ary.size_bytes = 0 ;

    return(retval) ;

    /*END of ITI_ATTR_init_table()*/
}