erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.1 KiB

5 years ago
  1. #include "neural_utils.h"
  2. unsigned long int estimate_size(ErlNifEnv *env, ERL_NIF_TERM term) {
  3. if (enif_is_atom(env, term)) {
  4. return WORD_SIZE;
  5. }
  6. // Treating all numbers like longs.
  7. if (enif_is_number(env, term)) {
  8. return 2 * WORD_SIZE;
  9. }
  10. if (enif_is_binary(env, term)) {
  11. ErlNifBinary bin;
  12. enif_inspect_binary(env, term, &bin);
  13. return bin.size + (6 * WORD_SIZE);
  14. }
  15. if (enif_is_list(env, term)) {
  16. unsigned long int size = 0;
  17. ERL_NIF_TERM it, curr;
  18. it = term;
  19. size += WORD_SIZE;
  20. while (!enif_is_empty_list(env, it)) {
  21. enif_get_list_cell(env, it, &curr, &it);
  22. size += estimate_size(env, curr) + WORD_SIZE;
  23. }
  24. return size;
  25. }
  26. if (enif_is_tuple(env, term)) {
  27. unsigned long int size = 0;
  28. const ERL_NIF_TERM *tpl;
  29. int arity;
  30. enif_get_tuple(env, term, &arity, &tpl);
  31. for (int i = 0; i < arity; ++i) {
  32. size += estimate_size(env, tpl[i]);
  33. }
  34. return size;
  35. }
  36. // Return 1 word by default
  37. return WORD_SIZE;
  38. }