go-make-slice.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* go-make-slice.c -- make a slice.
  2. Copyright 2011 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #include <stdint.h>
  6. #include "runtime.h"
  7. #include "go-alloc.h"
  8. #include "go-assert.h"
  9. #include "go-panic.h"
  10. #include "go-type.h"
  11. #include "array.h"
  12. #include "arch.h"
  13. #include "malloc.h"
  14. /* Dummy word to use as base pointer for make([]T, 0).
  15. Since you cannot take the address of such a slice,
  16. you can't tell that they all have the same base pointer. */
  17. uintptr runtime_zerobase;
  18. struct __go_open_array
  19. __go_make_slice2 (const struct __go_type_descriptor *td, uintptr_t len,
  20. uintptr_t cap)
  21. {
  22. const struct __go_slice_type* std;
  23. intgo ilen;
  24. intgo icap;
  25. uintptr_t size;
  26. struct __go_open_array ret;
  27. __go_assert ((td->__code & GO_CODE_MASK) == GO_SLICE);
  28. std = (const struct __go_slice_type *) td;
  29. ilen = (intgo) len;
  30. if (ilen < 0
  31. || (uintptr_t) ilen != len
  32. || (std->__element_type->__size > 0
  33. && len > MaxMem / std->__element_type->__size))
  34. runtime_panicstring ("makeslice: len out of range");
  35. icap = (intgo) cap;
  36. if (cap < len
  37. || (uintptr_t) icap != cap
  38. || (std->__element_type->__size > 0
  39. && cap > MaxMem / std->__element_type->__size))
  40. runtime_panicstring ("makeslice: cap out of range");
  41. ret.__count = ilen;
  42. ret.__capacity = icap;
  43. size = cap * std->__element_type->__size;
  44. if (size == 0)
  45. ret.__values = &runtime_zerobase;
  46. else if ((std->__element_type->__code & GO_NO_POINTERS) != 0)
  47. ret.__values =
  48. runtime_mallocgc (size,
  49. (uintptr) std->__element_type | TypeInfo_Array,
  50. FlagNoScan);
  51. else
  52. ret.__values =
  53. runtime_mallocgc (size,
  54. (uintptr) std->__element_type | TypeInfo_Array,
  55. 0);
  56. return ret;
  57. }
  58. struct __go_open_array
  59. __go_make_slice1 (const struct __go_type_descriptor *td, uintptr_t len)
  60. {
  61. return __go_make_slice2 (td, len, len);
  62. }
  63. struct __go_open_array
  64. __go_make_slice2_big (const struct __go_type_descriptor *td, uint64_t len,
  65. uint64_t cap)
  66. {
  67. uintptr_t slen;
  68. uintptr_t scap;
  69. slen = (uintptr_t) len;
  70. if ((uint64_t) slen != len)
  71. runtime_panicstring ("makeslice: len out of range");
  72. scap = (uintptr_t) cap;
  73. if ((uint64_t) scap != cap)
  74. runtime_panicstring ("makeslice: cap out of range");
  75. return __go_make_slice2 (td, slen, scap);
  76. }
  77. struct __go_open_array
  78. __go_make_slice1_big (const struct __go_type_descriptor *td, uint64_t len)
  79. {
  80. return __go_make_slice2_big (td, len, len);
  81. }