Changeset d31a365 in rtems-libbsd
- Timestamp:
- Jul 14, 2016, 9:01:43 AM (5 years ago)
- Branches:
- b96abfd647154f10ea8f7fac68e25676636eded5, debc0aed8b9bef9996b8ec0ae7a70b0b41912e27, 68e79b6d187fe09b0a6f3f1d22c69b4121a3f76e, freebsd-9.3, 33bfaee89aa71d2252eb48d6b9a9ec17183faced
- Children:
- 21abaef
- Parents:
- 3d91340
- git-author:
- Christian Mauderer <Christian.Mauderer@…> (07/14/16 09:01:43)
- git-committer:
- Christian Mauderer <Christian.Mauderer@…> (08/02/16 08:21:46)
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
rtemsbsd/include/machine/rtems-bsd-program.h
r3d91340 rd31a365 44 44 #include <stdbool.h> 45 45 #include <stdio.h> 46 #include <stdarg.h> 46 47 47 48 __BEGIN_DECLS … … 57 58 rtems_bsd_program_call_main_with_data_restore(const char *name, 58 59 int (*main)(int, char **), int argc, char **argv, 59 constvoid *data_buf, const size_t data_size);60 void *data_buf, const size_t data_size); 60 61 61 62 void … … 77 78 rtems_bsd_program_unlock(void); 78 79 80 int 81 rtems_bsd_program_open(const char *path, int oflag, ...); 82 83 int 84 rtems_bsd_program_socket(int domain, int type, int protocol); 85 86 int 87 rtems_bsd_program_close(int fd); 88 89 FILE * 90 rtems_bsd_program_fopen(const char *restrict filename, 91 const char *restrict mode); 92 93 int 94 rtems_bsd_program_fclose(FILE *file); 95 96 void * 97 rtems_bsd_program_malloc(size_t size); 98 99 void * 100 rtems_bsd_program_calloc(size_t nelem, size_t elsize); 101 102 void * 103 rtems_bsd_program_realloc(void *ptr, size_t size); 104 105 char * 106 rtems_bsd_program_strdup(const char *s1); 107 108 int 109 rtems_bsd_program_vasprintf(char **strp, const char *fmt, va_list ap); 110 111 int 112 rtems_bsd_program_asprintf(char **strp, const char *fmt, ...); 113 114 void 115 rtems_bsd_program_free(void *ptr); 116 79 117 #ifndef RTEMS_BSD_PROGRAM_NO_EXIT_WRAP 80 118 #define exit(code) rtems_bsd_program_exit(code) … … 93 131 #endif 94 132 133 #ifndef RTEMS_BSD_PROGRAM_NO_OPEN_WRAP 134 #define open(path, oflag, ...) \ 135 rtems_bsd_program_open(path, oflag, ## __VA_ARGS__) 136 #endif 137 138 #ifndef RTEMS_BSD_PROGRAM_NO_SOCKET_WRAP 139 #define socket(domain, type, protocol) \ 140 rtems_bsd_program_socket(domain, type, protocol) 141 #endif 142 143 #ifndef RTEMS_BSD_PROGRAM_NO_CLOSE_WRAP 144 #define close(fildes) rtems_bsd_program_close(fildes) 145 #endif 146 147 #ifndef RTEMS_BSD_PROGRAM_NO_FOPEN_WRAP 148 #define fopen(filename, mode) rtems_bsd_program_fopen(filename, mode) 149 #endif 150 151 #ifndef RTEMS_BSD_PROGRAM_NO_FCLOSE_WRAP 152 #define fclose(file) rtems_bsd_program_fclose(file) 153 #endif 154 155 #ifndef RTEMS_BSD_PROGRAM_NO_MALLOC_WRAP 156 #define malloc(size) rtems_bsd_program_malloc(size) 157 #endif 158 159 #ifndef RTEMS_BSD_PROGRAM_NO_CALLOC_WRAP 160 #define calloc(nelem, elsize) rtems_bsd_program_calloc(nelem, elsize) 161 #endif 162 163 #ifndef RTEMS_BSD_PROGRAM_NO_REALLOC_WRAP 164 #define realloc(ptr, size) rtems_bsd_program_realloc(ptr, size) 165 #endif 166 167 #ifndef RTEMS_BSD_PROGRAM_NO_STRDUP_WRAP 168 #define strdup(s1) rtems_bsd_program_strdup(s1) 169 #endif 170 171 #ifndef RTEMS_BSD_PROGRAM_NO_VASPRINTF_WRAP 172 #define vasprintf(strp, fmt, ap) \ 173 rtems_bsd_program_vasprintf(strp, fmt, ap) 174 #endif 175 176 #ifndef RTEMS_BSD_PROGRAM_NO_ASPRINTF_WRAP 177 #define asprintf(strp, fmt, ...) \ 178 rtems_bsd_program_asprintf(strp, fmt, ## __VA_ARGS__) 179 #endif 180 181 #ifndef RTEMS_BSD_PROGRAM_NO_FREE_WRAP 182 #define free(ptr) rtems_bsd_program_free(ptr); 183 #endif 184 95 185 __END_DECLS 96 186 -
rtemsbsd/rtems/rtems-kernel-program.c
r3d91340 rd31a365 43 43 #include <rtems/bsd/sys/param.h> 44 44 #include <sys/types.h> 45 #include <sys/socket.h> 45 46 #include <sys/systm.h> 46 47 #include <sys/kernel.h> … … 50 51 #include <sys/mutex.h> 51 52 53 #include <fcntl.h> 52 54 #include <setjmp.h> 53 55 #include <stdarg.h> 54 56 #include <stdlib.h> 57 #include <string.h> 58 #include <syslog.h> 55 59 56 60 #undef printf 61 #define RTEMS_BSD_PROGRAM_NO_OPEN_WRAP 62 #define RTEMS_BSD_PROGRAM_NO_SOCKET_WRAP 63 #define RTEMS_BSD_PROGRAM_NO_CLOSE_WRAP 64 #define RTEMS_BSD_PROGRAM_NO_FOPEN_WRAP 65 #define RTEMS_BSD_PROGRAM_NO_FCLOSE_WRAP 66 #define RTEMS_BSD_PROGRAM_NO_MALLOC_WRAP 67 #define RTEMS_BSD_PROGRAM_NO_CALLOC_WRAP 68 #define RTEMS_BSD_PROGRAM_NO_REALLOC_WRAP 69 #define RTEMS_BSD_PROGRAM_NO_STRDUP_WRAP 70 #define RTEMS_BSD_PROGRAM_NO_VASPRINTF_WRAP 71 #define RTEMS_BSD_PROGRAM_NO_ASPRINTF_WRAP 72 #define RTEMS_BSD_PROGRAM_NO_FREE_WRAP 57 73 #include <machine/rtems-bsd-program.h> 58 74 … … 62 78 const char *name; 63 79 jmp_buf return_context; 80 LIST_HEAD(, rtems_bsd_fd_list) open_fd; 81 LIST_HEAD(, rtems_bsd_file_list) open_file; 82 LIST_HEAD(, rtems_bsd_allocmem_list) allocated_mem; 64 83 }; 84 85 struct rtems_bsd_fd_list { 86 int fd; 87 LIST_ENTRY(rtems_bsd_fd_list) entries; 88 }; 89 90 struct rtems_bsd_file_list { 91 FILE *file; 92 LIST_ENTRY(rtems_bsd_file_list) entries; 93 }; 94 95 struct rtems_bsd_allocmem_list { 96 void *ptr; 97 LIST_ENTRY(rtems_bsd_allocmem_list) entries; 98 }; 99 100 enum rtems_bsd_alloc_type { 101 RTEMS_BSD_ALLOC_CALL_MALLOC, 102 RTEMS_BSD_ALLOC_CALL_CALLOC, 103 RTEMS_BSD_ALLOC_CALL_REALLOC, 104 RTEMS_BSD_ALLOC_CALL_STRDUP, 105 }; 106 107 static struct rtems_bsd_program_control * 108 rtems_bsd_program_get_cur_prog_ctrl_or_null(void) 109 { 110 struct rtems_bsd_program_control *prog_ctrl = NULL; 111 struct thread *td = rtems_bsd_get_curthread_or_null(); 112 113 if (td != NULL) { 114 prog_ctrl = td->td_prog_ctrl; 115 116 if (prog_ctrl == NULL) { 117 panic("unexpected BSD program state"); 118 } 119 } else { 120 errno = ENOMEM; 121 } 122 123 return prog_ctrl; 124 } 125 126 static int 127 fd_remove(struct rtems_bsd_program_control *prog_ctrl, int fd) 128 { 129 struct rtems_bsd_fd_list *current; 130 int rv = -1; 131 132 for(current = prog_ctrl->open_fd.lh_first; 133 current != NULL; 134 current = current->entries.le_next) { 135 if(current->fd == fd) { 136 LIST_REMOVE(current, entries); 137 free(current, M_TEMP); 138 rv = 0; 139 break; 140 } 141 } 142 143 return rv; 144 } 145 146 static int 147 fd_close_remove(struct rtems_bsd_program_control *prog_ctrl, int fd) 148 { 149 int rv = -1; 150 151 rv = fd_remove(prog_ctrl, fd); 152 if(rv == 0) { 153 rv = close(fd); 154 } 155 156 return rv; 157 } 158 159 static void 160 fd_close_all(struct rtems_bsd_program_control *prog_ctrl) 161 { 162 while(prog_ctrl->open_fd.lh_first != NULL) { 163 struct rtems_bsd_fd_list *current; 164 int fd; 165 int rv; 166 167 current = prog_ctrl->open_fd.lh_first; 168 fd = current->fd; 169 170 rv = fd_close_remove(prog_ctrl, fd); 171 if(rv != 0) { 172 syslog(LOG_ERR, 173 "BSD Program: Could not close file %d or could not remove it from list of open files", 174 fd); 175 } 176 } 177 } 178 179 static int 180 file_remove(struct rtems_bsd_program_control *prog_ctrl, FILE *file) 181 { 182 struct rtems_bsd_file_list *current; 183 int rv = EOF; 184 185 for(current = prog_ctrl->open_file.lh_first; 186 current != NULL; 187 current = current->entries.le_next) { 188 if(current->file == file) { 189 LIST_REMOVE(current, entries); 190 free(current, M_TEMP); 191 rv = 0; 192 break; 193 } 194 } 195 196 return rv; 197 } 198 199 static int 200 file_close_remove(struct rtems_bsd_program_control *prog_ctrl, FILE *file) 201 { 202 int rv = EOF; 203 204 rv = file_remove(prog_ctrl, file); 205 if(rv == 0) { 206 rv = fclose(file); 207 } 208 209 return rv; 210 } 211 212 static void 213 file_close_all(struct rtems_bsd_program_control *prog_ctrl) 214 { 215 while(prog_ctrl->open_file.lh_first != NULL) { 216 struct rtems_bsd_file_list *current; 217 FILE *file; 218 int rv; 219 220 current = prog_ctrl->open_file.lh_first; 221 file = current->file; 222 223 rv = file_close_remove(prog_ctrl, file); 224 if(rv != 0) { 225 syslog(LOG_ERR, 226 "BSD Program: Could not close stream %p or could not remove it from list of open streams", 227 file); 228 } 229 } 230 } 231 232 static int 233 allocmem_remove(struct rtems_bsd_program_control *prog_ctrl, void *ptr) 234 { 235 struct rtems_bsd_allocmem_list *current; 236 int rv = -1; 237 238 for(current = prog_ctrl->allocated_mem.lh_first; 239 current != NULL; 240 current = current->entries.le_next) { 241 if(current->ptr == ptr) { 242 LIST_REMOVE(current, entries); 243 rv = 0; 244 break; 245 } 246 } 247 248 return rv; 249 } 250 251 static int 252 allocmem_free_remove(struct rtems_bsd_program_control *prog_ctrl, void *ptr) 253 { 254 int rv = -1; 255 256 rv = allocmem_remove(prog_ctrl, ptr); 257 if(rv == 0) { 258 free(ptr, M_TEMP); 259 } 260 261 return rv; 262 } 263 264 static void 265 allocmem_free_all(struct rtems_bsd_program_control *prog_ctrl) 266 { 267 while(prog_ctrl->allocated_mem.lh_first != NULL) { 268 struct rtems_bsd_allocmem_list *current; 269 void *ptr; 270 int rv; 271 272 current = prog_ctrl->allocated_mem.lh_first; 273 ptr = current->ptr; 274 275 rv = allocmem_free_remove(prog_ctrl, ptr); 276 if(rv != 0) { 277 /* This should never happen. It would mean that someone 278 * changed the allocmem list while we are removing the 279 * element. */ 280 syslog(LOG_ERR, 281 "BSD Program: Could not remove %p from list of allocated memory", 282 ptr); 283 } 284 } 285 } 65 286 66 287 int … … 82 303 prog_ctrl->name = name; 83 304 prog_ctrl->exit_code = exit_code; 305 306 LIST_INIT(&(prog_ctrl->open_fd)); 307 LIST_INIT(&(prog_ctrl->open_file)); 308 LIST_INIT(&(prog_ctrl->allocated_mem)); 84 309 85 310 if (setjmp(prog_ctrl->return_context) == 0) { … … 88 313 exit_code = prog_ctrl->exit_code; 89 314 } 315 316 fd_close_all(prog_ctrl); 317 file_close_all(prog_ctrl); 318 allocmem_free_all(prog_ctrl); 90 319 91 320 td->td_prog_ctrl = NULL; … … 204 433 rtems_bsd_program_call_main_with_data_restore(const char *name, 205 434 int (*main)(int, char **), int argc, char **argv, 206 constvoid *data_buf, const size_t data_size)435 void *data_buf, const size_t data_size) 207 436 { 208 437 int exit_code = EXIT_FAILURE; … … 239 468 mtx_unlock(&program_mtx); 240 469 } 470 471 int 472 rtems_bsd_program_open(const char *path, int oflag, ...) 473 { 474 struct rtems_bsd_program_control *prog_ctrl = 475 rtems_bsd_program_get_cur_prog_ctrl_or_null(); 476 va_list list; 477 mode_t mode = 0; 478 int fd = -1; 479 480 if (prog_ctrl != NULL) { 481 struct rtems_bsd_fd_list *new = 482 malloc(sizeof(struct rtems_bsd_fd_list), M_TEMP, 0); 483 484 if(new != NULL) { 485 va_start(list, oflag); 486 mode = va_arg(list, mode_t); 487 488 fd = open(path, oflag, mode); 489 490 va_end(list); 491 492 if(fd != -1) { 493 new->fd = fd; 494 LIST_INSERT_HEAD(&(prog_ctrl->open_fd), 495 new, entries); 496 } else { 497 free(new, M_TEMP); 498 } 499 } else { 500 errno = ENOMEM; 501 } 502 } 503 504 return fd; 505 } 506 507 int 508 rtems_bsd_program_socket(int domain, int type, int protocol) 509 { 510 struct rtems_bsd_program_control *prog_ctrl = 511 rtems_bsd_program_get_cur_prog_ctrl_or_null(); 512 int fd = -1; 513 514 if (prog_ctrl != NULL) { 515 struct rtems_bsd_fd_list *new = 516 malloc(sizeof(struct rtems_bsd_fd_list), M_TEMP, 0); 517 518 if(new != NULL) { 519 /* FIXME: Why is there an implicit declaration warning? 520 */ 521 fd = socket(domain, type, protocol); 522 523 if(fd != -1) { 524 new->fd = fd; 525 LIST_INSERT_HEAD(&(prog_ctrl->open_fd), 526 new, entries); 527 } else { 528 free(new, M_TEMP); 529 } 530 } else { 531 errno = ENOMEM; 532 } 533 } 534 535 return fd; 536 } 537 538 int 539 rtems_bsd_program_close(int fd) 540 { 541 struct rtems_bsd_program_control *prog_ctrl = 542 rtems_bsd_program_get_cur_prog_ctrl_or_null(); 543 int rv = -1; 544 545 if (prog_ctrl != NULL) { 546 rv = fd_close_remove(prog_ctrl, fd); 547 } 548 549 return rv; 550 } 551 552 FILE * 553 rtems_bsd_program_fopen(const char *restrict filename, 554 const char *restrict mode) 555 { 556 FILE *file = NULL; 557 struct rtems_bsd_program_control *prog_ctrl = 558 rtems_bsd_program_get_cur_prog_ctrl_or_null(); 559 560 if (prog_ctrl != NULL) { 561 struct rtems_bsd_file_list *new = 562 malloc(sizeof(struct rtems_bsd_file_list), M_TEMP, 0); 563 564 if(new != NULL) { 565 file = fopen(filename, mode); 566 567 if(file != NULL) { 568 new->file = file; 569 LIST_INSERT_HEAD( 570 &(prog_ctrl->open_file), new, 571 entries); 572 } else { 573 free(new, M_TEMP); 574 } 575 } else { 576 errno = ENOMEM; 577 } 578 } 579 580 return file; 581 } 582 583 int 584 rtems_bsd_program_fclose(FILE *file) 585 { 586 struct rtems_bsd_program_control *prog_ctrl = 587 rtems_bsd_program_get_cur_prog_ctrl_or_null(); 588 int rv = EOF; 589 590 if (prog_ctrl != NULL) { 591 rv = file_close_remove(prog_ctrl, file); 592 } 593 594 return rv; 595 } 596 597 static void * 598 rtems_bsd_program_alloc(size_t size, void *org_ptr) 599 { 600 struct rtems_bsd_program_control *prog_ctrl = 601 rtems_bsd_program_get_cur_prog_ctrl_or_null(); 602 void *ptr = NULL; 603 size_t size_with_list; 604 size_t size_alligned; 605 606 if (prog_ctrl != NULL) { 607 /* align the end to the next word address */ 608 size_alligned = size; 609 if((size_alligned & 0x3) != 0) { 610 size_alligned = (size_alligned | 0x03) + 1; 611 } 612 size_with_list = size_alligned + 613 sizeof(struct rtems_bsd_allocmem_list); 614 615 if(org_ptr != NULL) { 616 /* It's a reallocation. So first remove the old pointer 617 * from the list */ 618 allocmem_remove(prog_ctrl, org_ptr); 619 } 620 621 ptr = realloc(org_ptr, size_with_list, M_TEMP, 0); 622 623 if(ptr != NULL) { 624 struct rtems_bsd_allocmem_list *listele; 625 listele = ptr + size_alligned; 626 listele->ptr = ptr; 627 LIST_INSERT_HEAD(&(prog_ctrl->allocated_mem), 628 listele, entries); 629 } 630 } 631 632 return ptr; 633 } 634 635 void * 636 rtems_bsd_program_malloc(size_t size) 637 { 638 return rtems_bsd_program_alloc(size, NULL); 639 } 640 641 void * 642 rtems_bsd_program_calloc(size_t nelem, size_t elsize) 643 { 644 void *ptr; 645 size_t size = elsize * nelem; 646 647 ptr = rtems_bsd_program_alloc(size, NULL); 648 if(ptr != NULL) { 649 memset(ptr, 0, size); 650 } 651 652 return ptr; 653 } 654 655 void * 656 rtems_bsd_program_realloc(void *ptr, size_t size) 657 { 658 return rtems_bsd_program_alloc(size, ptr); 659 } 660 661 char * 662 rtems_bsd_program_strdup(const char *s1) 663 { 664 size_t size = strlen(s1) + 1; /* add one for null termination */ 665 char *new; 666 667 new = rtems_bsd_program_alloc(size, NULL); 668 669 if(new != NULL) { 670 memcpy(new, s1, size); 671 } 672 673 return new; 674 } 675 676 int 677 rtems_bsd_program_vasprintf(char **strp, const char *fmt, va_list ap) 678 { 679 va_list aq; 680 int size; 681 int rv = -1; 682 683 va_copy(aq, ap); 684 size = vsnprintf(NULL, 0, fmt, aq); 685 va_end(aq); 686 687 size += 1; /* Add space for terminating null byte */ 688 *strp = rtems_bsd_program_alloc(size, NULL); 689 690 if(*strp != NULL) { 691 rv = vsnprintf(*strp, size, fmt, ap); 692 } 693 694 return rv; 695 } 696 697 int 698 rtems_bsd_program_asprintf(char **strp, const char *fmt, ...) 699 { 700 va_list ap; 701 int rv; 702 703 va_start(ap, fmt); 704 705 rv = rtems_bsd_program_vasprintf(strp, fmt, ap); 706 707 va_end(ap); 708 709 return rv; 710 } 711 712 void 713 rtems_bsd_program_free(void *ptr) 714 { 715 if(ptr != NULL) { 716 struct rtems_bsd_program_control *prog_ctrl = 717 rtems_bsd_program_get_cur_prog_ctrl_or_null(); 718 719 if (prog_ctrl != NULL) { 720 int rv = allocmem_free_remove(prog_ctrl, ptr); 721 if(rv != 0) { 722 panic("unexpected BSD program state: could not find pointer to free"); 723 } 724 } else { 725 /* Outside of program context. Just free it. */ 726 free(ptr, M_TEMP); 727 } 728 } 729 } -
testsuite/program01/test_main.c
r3d91340 rd31a365 49 49 #define TEST_NAME "LIBBSD SYSCALLS 1" 50 50 51 struct alloc_ctx { 52 enum alloc_type { 53 ALLOC_MALLOC = 0, 54 ALLOC_CALLOC = 1, 55 ALLOC_REALLOC = 2, 56 ALLOC_STRDUP = 3, 57 ALLOC_ASPRINTF = 4, 58 ALLOC_LAST = 5, 59 } type; 60 unsigned free; 61 #define ALLOC_FREE_NONE 0x0 62 #define ALLOC_FREE_FIRST 0x1 63 #define ALLOC_FREE_SECOND 0x2 64 #define ALLOC_FREE_THIRD 0x4 65 #define ALLOC_FREE_ALL (ALLOC_FREE_FIRST | \ 66 ALLOC_FREE_SECOND | \ 67 ALLOC_FREE_THIRD) 68 }; 69 51 70 typedef void (*no_mem_test_body)(int fd); 52 71 … … 340 359 } 341 360 361 static int 362 call_socket(void *ctx) 363 { 364 int fd = socket(PF_INET, SOCK_DGRAM, 0); 365 assert (fd != -1); 366 return 0; 367 } 368 369 static int 370 call_socket_close(void *ctx) 371 { 372 int rv; 373 int fd = socket(PF_INET, SOCK_DGRAM, 0); 374 assert (fd != -1); 375 376 rv = close(fd); 377 assert(rv == 0); 378 379 return 0; 380 } 381 382 static int 383 call_open(void *ctx) 384 { 385 int fd = open("/testfile", O_RDWR | O_CREAT, S_IRWXU); 386 assert (fd != -1); 387 return 0; 388 } 389 390 static int 391 call_open_close(void *ctx) 392 { 393 int rv; 394 int fd = open("/testfile", O_RDWR); 395 assert (fd != -1); 396 397 rv = close(fd); 398 assert(rv == 0); 399 400 return 0; 401 } 402 403 static int 404 call_fopen(void *ctx) 405 { 406 FILE *file = fopen("/testfile", "rw"); 407 assert (file != NULL); 408 return 0; 409 } 410 411 static int 412 call_fopen_fclose(void *ctx) 413 { 414 int rv; 415 416 FILE *file = fopen("/testfile", "rw"); 417 assert (file != NULL); 418 419 rv = fclose(file); 420 assert(rv == 0); 421 422 return 0; 423 } 424 425 static void 426 test_open_close(void) 427 { 428 int exit_code; 429 rtems_resource_snapshot snapshot; 430 431 puts("test open, socket and close"); 432 433 /* Call a first time to create all resources before taking a memory 434 * snapshot. */ 435 exit_code = rtems_bsd_program_call("socket", call_socket, NULL); 436 assert(exit_code == 0); 437 exit_code = rtems_bsd_program_call("open", call_open, NULL); 438 assert(exit_code == 0); 439 exit_code = rtems_bsd_program_call("fopen", call_fopen, NULL); 440 assert(exit_code == 0); 441 442 rtems_resource_snapshot_take(&snapshot); 443 444 exit_code = rtems_bsd_program_call("open", call_open, NULL); 445 assert(exit_code == 0); 446 assert(rtems_resource_snapshot_check(&snapshot)); 447 448 exit_code = rtems_bsd_program_call("open_close", call_open_close, NULL); 449 assert(exit_code == 0); 450 assert(rtems_resource_snapshot_check(&snapshot)); 451 452 rtems_resource_snapshot_take(&snapshot); 453 454 exit_code = rtems_bsd_program_call("socket", call_socket, NULL); 455 assert(exit_code == 0); 456 assert(rtems_resource_snapshot_check(&snapshot)); 457 458 exit_code = rtems_bsd_program_call("socket_close", call_socket_close, 459 NULL); 460 assert(exit_code == 0); 461 assert(rtems_resource_snapshot_check(&snapshot)); 462 463 exit_code = rtems_bsd_program_call("fopen", call_fopen, NULL); 464 assert(exit_code == 0); 465 assert(rtems_resource_snapshot_check(&snapshot)); 466 467 exit_code = rtems_bsd_program_call("fopen_fclose", call_fopen_fclose, 468 NULL); 469 assert(exit_code == 0); 470 assert(rtems_resource_snapshot_check(&snapshot)); 471 } 472 473 static int 474 call_alloc(void *ctx) 475 { 476 struct alloc_ctx *context = ctx; 477 char *first, *second, *third; 478 const int random_size = 64; 479 const char teststring[] = "test"; 480 481 switch(context->type) { 482 case ALLOC_MALLOC: 483 first = malloc(random_size * sizeof(int)); 484 second = malloc(random_size * sizeof(int)); 485 third = malloc(random_size * sizeof(int)); 486 break; 487 case ALLOC_CALLOC: 488 first = calloc(random_size, sizeof(int)); 489 second = calloc(random_size, sizeof(int)); 490 third = calloc(random_size, sizeof(int)); 491 break; 492 case ALLOC_REALLOC: 493 first = malloc(sizeof(int)); 494 second = malloc(sizeof(int)); 495 third = malloc(sizeof(int)); 496 assert(first != NULL); 497 assert(second != NULL); 498 assert(third != NULL); 499 first = realloc(first, sizeof(int) * random_size); 500 second = realloc(second, sizeof(int) * random_size); 501 third = realloc(third, sizeof(int) * random_size); 502 break; 503 case ALLOC_STRDUP: 504 first = strdup(teststring); 505 second = strdup(teststring); 506 third = strdup(teststring); 507 break; 508 case ALLOC_ASPRINTF: 509 asprintf(&first, "a number %d", 0x123456); 510 asprintf(&second, "some string: %s", "abcdefghijklm"); 511 asprintf(&third, "just something"); 512 break; 513 default: 514 assert(false); 515 break; 516 } 517 518 assert(first != NULL); 519 assert(second != NULL); 520 assert(third != NULL); 521 522 if((context->free & ALLOC_FREE_FIRST) != 0) { 523 free(first); 524 } 525 if((context->free & ALLOC_FREE_SECOND) != 0) { 526 free(second); 527 } 528 if((context->free & ALLOC_FREE_THIRD) != 0) { 529 free(third); 530 } 531 532 return 0; 533 } 534 535 static int 536 call_free_on_null(void *ctx) 537 { 538 void *new = NULL; 539 free(new); 540 return 0; 541 } 542 543 static void 544 test_alloc_free(void) 545 { 546 int exit_code; 547 rtems_resource_snapshot snapshot; 548 struct alloc_ctx context; 549 enum alloc_type type; 550 551 puts("test alloc and free"); 552 553 rtems_resource_snapshot_take(&snapshot); 554 555 for(type = ALLOC_MALLOC; type < ALLOC_LAST; ++type) { 556 unsigned free; 557 558 for(free = ALLOC_FREE_NONE; free < ALLOC_FREE_ALL; ++free) { 559 context.type = type; 560 context.free = free; 561 562 exit_code = rtems_bsd_program_call("alloc", call_alloc, 563 &context); 564 assert(exit_code == 0); 565 assert(rtems_resource_snapshot_check(&snapshot)); 566 } 567 } 568 569 exit_code = rtems_bsd_program_call("free_on_null", call_free_on_null, 570 NULL); 571 assert(exit_code == 0); 572 assert(rtems_resource_snapshot_check(&snapshot)); 573 } 574 342 575 static void 343 576 test_main(void) … … 346 579 test_warn(); 347 580 test_err(); 581 test_open_close(); 582 test_alloc_free(); 348 583 349 584 exit(0);
Note: See TracChangeset
for help on using the changeset viewer.