dispatch_data.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2009-2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <dispatch/dispatch.h>
  23. #ifdef __APPLE__
  24. #include <TargetConditionals.h>
  25. #endif
  26. #include <bsdtests.h>
  27. #include "dispatch_test.h"
  28. #ifndef DISPATCHTEST_DATA
  29. #if DISPATCH_API_VERSION >= 20100226 && DISPATCH_API_VERSION != 20101110
  30. #define DISPATCHTEST_DATA 1
  31. #endif
  32. #endif
  33. dispatch_group_t g;
  34. #if DISPATCHTEST_DATA
  35. static void
  36. test_concat(void)
  37. {
  38. dispatch_group_enter(g);
  39. dispatch_async(dispatch_get_main_queue(), ^{
  40. const char* buffer1 = "This is buffer1 ";
  41. size_t size1 = 17;
  42. const char* buffer2 = "This is buffer2 ";
  43. size_t size2 = 17;
  44. __block bool buffer2_destroyed = false;
  45. dispatch_data_t data1 = dispatch_data_create(buffer1, size1, NULL, NULL);
  46. dispatch_data_t data2 = dispatch_data_create(buffer2, size2,
  47. dispatch_get_main_queue(), ^{
  48. buffer2_destroyed = true;
  49. });
  50. dispatch_data_t concat = dispatch_data_create_concat(data1, data2);
  51. dispatch_release(data1);
  52. dispatch_release(data2);
  53. test_long("Data size of concatenated dispatch data",
  54. (long)dispatch_data_get_size(concat), 34);
  55. const void* contig;
  56. size_t contig_size;
  57. dispatch_data_t contig_data =
  58. dispatch_data_create_map(concat, &contig, &contig_size);
  59. dispatch_release(concat);
  60. dispatch_release(contig_data);
  61. test_long("Contiguous memory size", (long)contig_size, 34);
  62. dispatch_async(dispatch_get_main_queue(), ^{
  63. test_long("buffer2 destroyed", buffer2_destroyed, true);
  64. dispatch_group_leave(g);
  65. });
  66. });
  67. }
  68. static void
  69. test_cleanup(void) // <rdar://problem/9843440>
  70. {
  71. static char buffer4[16];
  72. dispatch_group_enter(g);
  73. dispatch_async(dispatch_get_main_queue(), ^{
  74. void *buffer3 = malloc(1024);
  75. dispatch_data_t data3 = dispatch_data_create(buffer3, 0,
  76. dispatch_get_main_queue(), DISPATCH_DATA_DESTRUCTOR_FREE);
  77. __block bool buffer4_destroyed = false;
  78. dispatch_data_t data4 = dispatch_data_create(buffer4, 16,
  79. dispatch_get_main_queue(), ^{
  80. buffer4_destroyed = true;
  81. });
  82. dispatch_release(data3);
  83. dispatch_release(data4);
  84. dispatch_async(dispatch_get_main_queue(), ^{
  85. test_long("buffer4 destroyed", buffer4_destroyed, true);
  86. dispatch_group_leave(g);
  87. });
  88. });
  89. }
  90. #endif
  91. int
  92. main(void)
  93. {
  94. dispatch_test_start("Dispatch Data");
  95. g = dispatch_group_create();
  96. #if DISPATCHTEST_DATA
  97. test_concat();
  98. test_cleanup();
  99. #endif
  100. dispatch_group_notify(g, dispatch_get_main_queue(), ^{
  101. dispatch_release(g);
  102. test_stop();
  103. });
  104. dispatch_main();
  105. }