mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Generic Linked List Library (like C++ STL list) - version 1.0
@ 2022-10-20  6:39 Amit
  2022-10-20  9:11 ` David Laight
  0 siblings, 1 reply; 2+ messages in thread
From: Amit @ 2022-10-20  6:39 UTC (permalink / raw)
  To: linux-kernel

Generic Linked List Library (like C++ STL list) - version 1.0

The code is below:

-------------------------------------
generic_linked_list_library.c
-------------------------------------

/*
 * License: This file has been released under APACHE LICENSE, VERSION 2.0.
 * The license details can be found here:
https://www.apache.org/licenses/LICENSE-2.0
 */

#include <stdlib.h>
#include <string.h>

#include "generic_linked_list_library.h"

struct generic_linked_list_container
*init_generic_linked_list_container(before_deleting_data_callback_function
func)
{

    struct generic_linked_list_container *gllc_ptr = malloc(sizeof(*gllc_ptr));

    if (!gllc_ptr)
        return NULL;

    gllc_ptr->first = NULL;
    gllc_ptr->last = NULL;
    gllc_ptr->bdd_callback_func = func;
    gllc_ptr->total_number_of_nodes = 0;

    return gllc_ptr;

} // end of init_generic_linked_list_container

int add_new_node_to_end(struct generic_linked_list_container
*gllc_ptr, void *data, long data_len)
{

    struct linked_list_node *lln = NULL;
    struct node_data *nddt = NULL;

    if (!data)
        return ARG_DATA_IS_NULL;

    if (data_len <= 0)
        return DATA_LEN_IS_INVALID;

    lln = malloc(sizeof(*lln));
    if (!lln) {
        return NO_MEMORY;
    }

    nddt = malloc(sizeof(*nddt));
    if (!nddt) {
        free(lln);
        return NO_MEMORY;
    }

    nddt->data = malloc((size_t)(data_len));
    if (!nddt->data) {
        free(nddt);
        free(lln);
        return NO_MEMORY;
    }

    nddt->data_len = data_len;
    memmove(nddt->data, data, (size_t)(nddt->data_len));

    lln->nd_ptr = nddt;
    lln->next = NULL;

    if (gllc_ptr->total_number_of_nodes == 0) {
        gllc_ptr->first = lln;
        gllc_ptr->last = lln;
    } else {
        gllc_ptr->last->next = lln;
    }

    gllc_ptr->total_number_of_nodes = gllc_ptr->total_number_of_nodes + 1;

    return GLLL_SUCCESS;

} // end of add_new_node_to_end

struct node_data *remove_front_node_and_get_data(struct
generic_linked_list_container *gllc_ptr)
{

    struct linked_list_node *lln_ptr = NULL;
    struct node_data *nddt = NULL;

    lln_ptr = remove_front_node(gllc_ptr);
    if (!lln_ptr)
        return NULL;

    nddt = lln_ptr->nd_ptr;

    free(lln_ptr);

    return nddt;

} // end of remove_front_node_and_get_data

struct linked_list_node *remove_front_node(struct
generic_linked_list_container *gllc_ptr)
{

    struct linked_list_node *lln_ptr = NULL;

    if (gllc_ptr->total_number_of_nodes == 0)
        return NULL;

    lln_ptr = gllc_ptr->first;

    if (gllc_ptr->total_number_of_nodes == 1) {
        gllc_ptr->first = NULL;
        gllc_ptr->last = NULL;
    } else {
        gllc_ptr->first = gllc_ptr->first->next;
    }

    gllc_ptr->total_number_of_nodes = gllc_ptr->total_number_of_nodes - 1;

    lln_ptr->next = NULL;

    return lln_ptr;

} // end of remove_front_node

void delete_front_node(struct generic_linked_list_container *gllc_ptr)
{

    struct linked_list_node *lln_ptr = NULL;
    struct node_data *nddt = NULL;

    lln_ptr = remove_front_node(gllc_ptr);
    if (!lln_ptr)
        return;

    nddt = lln_ptr->nd_ptr;

    if (gllc_ptr->bdd_callback_func)
        gllc_ptr->bdd_callback_func(nddt);

    free(nddt);
    free(lln_ptr);

    return;

} // end of delete_front_node

-------------------------------------
generic_linked_list_library.h
-------------------------------------

/*
 * License: This file has been released under APACHE LICENSE, VERSION 2.0.
 * The license details can be found here:
https://www.apache.org/licenses/LICENSE-2.0
 */

#ifndef _GENERIC_LINKED_LIST_LIBRARY_H_
#define _GENERIC_LINKED_LIST_LIBRARY_H_

#define GLLL_SUCCESS 0 // everything happened successfully
#define ARG_DATA_IS_NULL -1 // 'data' argument is NULL
#define DATA_LEN_IS_INVALID -2 // 'data_len' argument is <= 0
#define NO_MEMORY -3 // no memory available

struct node_data
{
    void *data;
    long data_len;
};

struct linked_list_node
{
    struct node_data *nd_ptr;
    struct linked_list_node *next;
};

typedef void (*before_deleting_data_callback_function)(struct node_data *);

struct generic_linked_list_container
{
    struct linked_list_node *first;
    struct linked_list_node *last;
    before_deleting_data_callback_function bdd_callback_func;
    long total_number_of_nodes;
};

struct generic_linked_list_container
*init_generic_linked_list_container(before_deleting_data_callback_function
func);
int add_new_node_to_end(struct generic_linked_list_container
*gllc_ptr, void *data, long data_len);
struct node_data *remove_front_node_and_get_data(struct
generic_linked_list_container *gllc_ptr);
struct linked_list_node *remove_front_node(struct
generic_linked_list_container *gllc_ptr);
void delete_front_node(struct generic_linked_list_container *gllc_ptr);

#endif

---- End of code ----

^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: Generic Linked List Library (like C++ STL list) - version 1.0
  2022-10-20  6:39 Generic Linked List Library (like C++ STL list) - version 1.0 Amit
@ 2022-10-20  9:11 ` David Laight
  0 siblings, 0 replies; 2+ messages in thread
From: David Laight @ 2022-10-20  9:11 UTC (permalink / raw)
  To: 'Amit', linux-kernel

From: Amit
> Sent: 20 October 2022 07:40
> 
> Generic Linked List Library (like C++ STL list) - version 1.0
> 
> The code is below:

Move all copies of it to /dev/null.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-20  9:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-20  6:39 Generic Linked List Library (like C++ STL list) - version 1.0 Amit
2022-10-20  9:11 ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®