src/java.desktop/unix/native/libawt_xawt/awt/list.c
branchihse-warnings-cflags-branch
changeset 56230 489867818774
parent 47216 71c04702a3d5
equal deleted inserted replaced
56229:0015bf3a82e0 56230:489867818774
    71 
    71 
    72 
    72 
    73 /** ------------------------------------------------------------------------
    73 /** ------------------------------------------------------------------------
    74         Sets the pointers of the specified list to NULL.
    74         Sets the pointers of the specified list to NULL.
    75     --------------------------------------------------------------------- **/
    75     --------------------------------------------------------------------- **/
    76 #if NeedFunctionPrototypes
       
    77 void zero_list(list_ptr lp)
    76 void zero_list(list_ptr lp)
    78 #else
       
    79 void zero_list(lp)
       
    80     list_ptr lp;
       
    81 #endif
       
    82 {
    77 {
    83     lp->next = NULL;
    78     lp->next = NULL;
    84     lp->ptr.item = NULL;
    79     lp->ptr.item = NULL;
    85 }
    80 }
    86 
    81 
    90         list, then mallocs a new list node onto the end of the list.
    85         list, then mallocs a new list node onto the end of the list.
    91         The item pointer in the new node is set to "item" passed in,
    86         The item pointer in the new node is set to "item" passed in,
    92         and the next pointer in the new node is set to NULL.
    87         and the next pointer in the new node is set to NULL.
    93         Returns 1 if successful, 0 if the malloc failed.
    88         Returns 1 if successful, 0 if the malloc failed.
    94     -------------------------------------------------------------------- **/
    89     -------------------------------------------------------------------- **/
    95 #if NeedFunctionPrototypes
       
    96 int32_t add_to_list(list_ptr lp, void *item)
    90 int32_t add_to_list(list_ptr lp, void *item)
    97 #else
       
    98 int32_t add_to_list(lp, item)
       
    99     list_ptr lp;
       
   100     void *item;
       
   101 #endif
       
   102 {
    91 {
   103     while (lp->next) {
    92     while (lp->next) {
   104         lp = lp->next;
    93         lp = lp->next;
   105     }
    94     }
   106     if ((lp->next = (list_ptr) malloc( sizeof( list_item))) == NULL) {
    95     if ((lp->next = (list_ptr) malloc( sizeof( list_item))) == NULL) {
   116 
   105 
   117 /** ------------------------------------------------------------------------
   106 /** ------------------------------------------------------------------------
   118         Creates a new list and sets its pointers to NULL.
   107         Creates a new list and sets its pointers to NULL.
   119         Returns a pointer to the new list.
   108         Returns a pointer to the new list.
   120     -------------------------------------------------------------------- **/
   109     -------------------------------------------------------------------- **/
   121 list_ptr new_list ()
   110 list_ptr new_list (void)
   122 {
   111 {
   123     list_ptr lp;
   112     list_ptr lp;
   124 
   113 
   125     if (lp = (list_ptr) malloc( sizeof( list_item))) {
   114     if (lp = (list_ptr) malloc( sizeof( list_item))) {
   126         lp->next = NULL;
   115         lp->next = NULL;
   138         If start_at_curr is FALSE, the first item in the new list is the
   127         If start_at_curr is FALSE, the first item in the new list is the
   139         same as the first item in the old list.  In either case, the
   128         same as the first item in the old list.  In either case, the
   140         curr pointer in the new list is the same as in the old list.
   129         curr pointer in the new list is the same as in the old list.
   141         Returns a pointer to the new list head.
   130         Returns a pointer to the new list head.
   142     -------------------------------------------------------------------- **/
   131     -------------------------------------------------------------------- **/
   143 #if NeedFunctionPrototypes
       
   144 list_ptr dup_list_head(list_ptr lp, int32_t start_at_curr)
   132 list_ptr dup_list_head(list_ptr lp, int32_t start_at_curr)
   145 #else
       
   146 list_ptr dup_list_head(lp, start_at_curr)
       
   147     list_ptr lp;
       
   148     int32_t start_at_curr;
       
   149 #endif
       
   150 {
   133 {
   151     list_ptr new_list;
   134     list_ptr new_list;
   152 
   135 
   153     if ((new_list = (list_ptr) malloc( sizeof( list_item))) == NULL) {
   136     if ((new_list = (list_ptr) malloc( sizeof( list_item))) == NULL) {
   154 
   137 
   162 
   145 
   163 
   146 
   164 /** ------------------------------------------------------------------------
   147 /** ------------------------------------------------------------------------
   165         Returns the number of items in the list.
   148         Returns the number of items in the list.
   166     -------------------------------------------------------------------- **/
   149     -------------------------------------------------------------------- **/
   167 #if NeedFunctionPrototypes
       
   168 uint32_t list_length(list_ptr lp)
   150 uint32_t list_length(list_ptr lp)
   169 #else
       
   170 uint32_t list_length(lp)
       
   171     list_ptr lp;
       
   172 #endif
       
   173 {
   151 {
   174     uint32_t count = 0;
   152     uint32_t count = 0;
   175 
   153 
   176     while (lp->next) {
   154     while (lp->next) {
   177         count++;
   155         count++;
   189         locations.  If a match is found, that node is deleted from the
   167         locations.  If a match is found, that node is deleted from the
   190         list.  Storage for the node is freed, but not for the item itself.
   168         list.  Storage for the node is freed, but not for the item itself.
   191         Returns a pointer to the item, so the caller can free it if it
   169         Returns a pointer to the item, so the caller can free it if it
   192         so desires.  If a match is not found, returns NULL.
   170         so desires.  If a match is not found, returns NULL.
   193     -------------------------------------------------------------------- **/
   171     -------------------------------------------------------------------- **/
   194 #if NeedFunctionPrototypes
       
   195 void *delete_from_list(list_ptr lp, void *item)
   172 void *delete_from_list(list_ptr lp, void *item)
   196 #else
       
   197 void *delete_from_list(lp, item)
       
   198     list_ptr lp;
       
   199     void *item;
       
   200 #endif
       
   201 {
   173 {
   202     list_ptr new_next;
   174     list_ptr new_next;
   203 
   175 
   204     while (lp->next) {
   176     while (lp->next) {
   205         if (lp->next->ptr.item == item) {
   177         if (lp->next->ptr.item == item) {
   220         Deletes each node in the list *except the head*.  This allows
   192         Deletes each node in the list *except the head*.  This allows
   221         the deletion of lists where the head is not malloced or created
   193         the deletion of lists where the head is not malloced or created
   222         with new_list().  If free_items is true, each item pointed to
   194         with new_list().  If free_items is true, each item pointed to
   223         from the node is freed, in addition to the node itself.
   195         from the node is freed, in addition to the node itself.
   224     -------------------------------------------------------------------- **/
   196     -------------------------------------------------------------------- **/
   225 #if NeedFunctionPrototypes
       
   226 void delete_list(list_ptr lp, int32_t free_items)
   197 void delete_list(list_ptr lp, int32_t free_items)
   227 #else
       
   228 void delete_list(lp, free_items)
       
   229     list_ptr lp;
       
   230     int32_t free_items;
       
   231 #endif
       
   232 {
   198 {
   233     list_ptr del_node;
   199     list_ptr del_node;
   234     void *item;
   200     void *item;
   235 
   201 
   236     while (lp->next) {
   202     while (lp->next) {
   242             free( item);
   208             free( item);
   243         }
   209         }
   244     }
   210     }
   245 }
   211 }
   246 
   212 
   247 #if NeedFunctionPrototypes
       
   248 void delete_list_destroying(list_ptr lp, void destructor(void *item))
   213 void delete_list_destroying(list_ptr lp, void destructor(void *item))
   249 #else
       
   250 void delete_list_destroying(lp, destructor)
       
   251     list_ptr lp;
       
   252     void (*destructor)();
       
   253 #endif
       
   254 {
   214 {
   255     list_ptr del_node;
   215     list_ptr del_node;
   256     void *item;
   216     void *item;
   257 
   217 
   258     while (lp->next) {
   218     while (lp->next) {
   270 /** ------------------------------------------------------------------------
   230 /** ------------------------------------------------------------------------
   271         Returns a ptr to the first *item* (not list node) in the list.
   231         Returns a ptr to the first *item* (not list node) in the list.
   272         Sets the list head node's curr ptr to the first node in the list.
   232         Sets the list head node's curr ptr to the first node in the list.
   273         Returns NULL if the list is empty.
   233         Returns NULL if the list is empty.
   274     -------------------------------------------------------------------- **/
   234     -------------------------------------------------------------------- **/
   275 #if NeedFunctionPrototypes
       
   276 void * first_in_list(list_ptr lp)
   235 void * first_in_list(list_ptr lp)
   277 #else
       
   278 void * first_in_list(lp)
       
   279     list_ptr lp;
       
   280 #endif
       
   281 {
   236 {
   282     if (! lp) {
   237     if (! lp) {
   283 
   238 
   284         return NULL;
   239         return NULL;
   285     }
   240     }
   292         Returns a ptr to the next *item* (not list node) in the list.
   247         Returns a ptr to the next *item* (not list node) in the list.
   293         Sets the list head node's curr ptr to the next node in the list.
   248         Sets the list head node's curr ptr to the next node in the list.
   294         first_in_list must have been called prior.
   249         first_in_list must have been called prior.
   295         Returns NULL if no next item.
   250         Returns NULL if no next item.
   296     -------------------------------------------------------------------- **/
   251     -------------------------------------------------------------------- **/
   297 #if NeedFunctionPrototypes
       
   298 void * next_in_list(list_ptr lp)
   252 void * next_in_list(list_ptr lp)
   299 #else
       
   300 void * next_in_list(lp)
       
   301     list_ptr lp;
       
   302 #endif
       
   303 {
   253 {
   304     if (! lp) {
   254     if (! lp) {
   305 
   255 
   306         return NULL;
   256         return NULL;
   307     }
   257     }
   310     }
   260     }
   311 
   261 
   312     return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL;
   262     return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL;
   313 }
   263 }
   314 
   264 
   315 #if NeedFunctionPrototypes
       
   316 int32_t list_is_empty(list_ptr lp)
   265 int32_t list_is_empty(list_ptr lp)
   317 #else
       
   318 int32_t list_is_empty(lp)
       
   319     list_ptr lp;
       
   320 #endif
       
   321 {
   266 {
   322     return (lp == NULL || lp->next == NULL);
   267     return (lp == NULL || lp->next == NULL);
   323 }
   268 }